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__.pyfile, can empty
- the module want create should modulename.pyfile 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__.pyfile inside both folders
- created mymodule.pyfile inpackacgedir
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
pythonlibsfolder?
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