Create and import a custom python package - import doesn't work in the root -
i'm totally new python, , want create first python library peronal uses.
i'm using python 2.7.5, , running idle interface.
so far, understood documentation , related questions that:
- python goes through list of directories listed in sys.path find scripts , libraries
- the package directory must contain
__init__.py
file, can empty - the module want create should
modulename.py
file code inside package directory
(sources: http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html --- https://docs.python.org/2/tutorial/modules.html)
and here tried fails:
- created personal package directory
c:\....\pythonlibs
- created subpackage dir
c:\....\pythonlibs\package
- created
__init__.py
file inside both folders - created
mymodule.py
file inpackacge
dir
and in idle used code:
import sys sys.path.append(r'c:\....\pythonlibs')
first issue:
currently have append every time enter idle. how can keep directory in sys.path permanently there lot of other directories there?
then tried importing 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 against documentation, why can't import root
pythonlibs
folder?
with line
sys.path.append(r'c:\....\pythonlibs')
you instructing interpreter start looking modules (libraries) in directory. since directory not contain pythonlibs
folder (the parent does), can't import it.
similarly - because contains module package
, can import it.
Comments
Post a Comment