Daniel Möller
Daniel Möller

Reputation: 86620

Create and import a custom python package - import doesn't work in the root

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:

(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:

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

Answers (1)

hruske
hruske

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

Related Questions