Reputation: 86620
I'm totally new to Python, and I want to create my first Python library for peronal uses.
I'm using Python 2.7.5, and running the IDLE interface.
So far, what I understood from the documentation and related questions is that:
__init__.py
file, which can be empty modulename.py
file with the code inside the package directory (Sources: http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html --- https://docs.python.org/2/tutorial/modules.html)
And here is what I tried that fails:
C:\....\pythonlibs
C:\....\pythonlibs\package
__init__.py
file inside both folders mymodule.py
file in the packacge
dir And then in the IDLE used this code:
import sys
sys.path.append(r'C:\....\pythonlibs')
First issue:
Currently I have to do this append every time I enter the IDLE. How can I keep the directory in sys.path permanently just as there are a lot of other directories there?
Then I tried importing my package:
import pythonlibs #fails!! why?
import pythonlibs.package #fails!! why?
import package #works
The error is: ImportError: No module named pythonlibs
Second issue?
This seems to be against the documentation, why can't I import from the root
pythonlibs
folder?
Upvotes: 0
Views: 283
Reputation: 2243
With line
sys.path.append(r'C:\....\pythonlibs')
you are instructing interpreter to start looking for modules (libraries) in this directory. Since this directory does not contain pythonlibs
folder (the parent does), it can't import it.
Similarly - because it contains the module package
, it can import it.
Upvotes: 2