Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Lior Dolinski
Lior Dolinski
3,905 Points

__init__.py isn't doing his job..

everytime i make a certain project i put an init.py file in the folder so python could find my module. But, when i import the file in the console, it tells me the module cant be found..

in that case i use sys.path.insert and manually insert my folder's path to the directory list..

what could be the problem? why is python not automatically know where my files are?


Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Can you please provide more information:

  • Are you running in a local window or in the workspaces console window?
  • What is the file structure (perhaps post the output of ls -R)?
  • In which directory are you starting the python shell?
  • How are you invoking the python shell?
  • What command are you typing (perhaps post a long of the python shell session)?


Lior Dolinski
Lior Dolinski
3,905 Points

Chris Freeman Im running in a local window (my regular shell) the python shell starts in C:\Python34

lets say i create a new folder on my desktop, i put an init.py file in her, and then i create a simple file with one class in it which i name 'lior'

class Lior():
    def __init__(self):
        self.s = ""

    def getString(self):
        self.s = raw_input()

i save the file and i type in the console: from lior import Lior

and it tells my the module doesn't exist.. python simply doesnt know that there are python files out there to be found...

any clues?


1 Answer

Firstly, you would need to run the import statement from one directory up from the new one you created.

Second, if you create an __init__.py file in a directory, it makes that directory a package. Any other files in that directory will be available by their file name, in addition to any methods or other 'names' in the __init__.py file itself.

So, if your structure is as follows:

|-- app.py
|-- package
    |-- __init__.py
    |-- lior.py

Then in app.py you could use the following:

from package import lior



from package.lior import Lior


Or if you don't use the lior.py file, and instead change the package directory to be named lior and put the Lior class in the __init__.py file, then you can use:

from lior import Lior