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!
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
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?
Thanks

Lior Dolinski
3,905 PointsChris 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?
thanks!
1 Answer

Iain Simmons
Treehouse Moderator 32,304 PointsFirstly, 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:
Desktop
|-- app.py
|-- package
|-- __init__.py
|-- lior.py
Then in app.py
you could use the following:
from package import lior
lior.Lior()
Or:
from package.lior import Lior
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
Lior()
Chris Freeman
Treehouse Moderator 68,390 PointsChris Freeman
Treehouse Moderator 68,390 PointsCan you please provide more information:
ls -R
)?Thanks!