importing module in __init__ ?

This is the place for queries that don't fit in any of the other categories.

importing module in __init__ ?

Postby vrkiller » Sat Jul 06, 2013 9:30 am

Code: Select all
class keylogger():
    def __init__(self):
        self.a=4
        import os
    def key(self):
        print(self.a)
        os.system("start")


a=keylogger()
a.key()

this code dosen't work. It thinks os system is a global name that is not declared
how do i import os in my module?
- VrKiller
User avatar
vrkiller
 
Posts: 19
Joined: Fri Jun 28, 2013 10:50 am
Location: Denmark, århus

Re: importing module in __init__ ?

Postby vrkiller » Sat Jul 06, 2013 10:07 am

if you look at this

code1:

Code: Select all
class keylogger():
    def __init__(self):
        self.a=4
        self.lol=os.times()
    def key(self):
        print(self.a)
        os.system("start")
        print(self.lol)


code2:

Code: Select all

import test
import os
handle=test.keylogger()

handle.key()


It simply isn't recongising os. How can i fix this?
- VrKiller
User avatar
vrkiller
 
Posts: 19
Joined: Fri Jun 28, 2013 10:50 am
Location: Denmark, århus

Re: importing module in __init__ ?

Postby hrs » Sat Jul 06, 2013 10:59 am

What traceback do you get?
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: importing module in __init__ ?

Postby micseydel » Sat Jul 06, 2013 5:24 pm

You need to import os at the top of "code 1".
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], dboxall123, Google [Bot], MichaelLig, snippsat and 5 guests