[Tutor] Importing files not located on the home directory or standardard library

Steven D'Aprano steve at pearwood.info
Fri Jun 18 16:48:59 CEST 2010

On Thu, 17 Jun 2010 05:10:32 pm Independent Learner wrote:

> Lets say I want to load a file called quiz located in C:\Users\Julius
> Hernandez\Desktop\Python2.6.5\Online Course\Chp.4
> How would I configure PYTHONPATH or .pth  or what ever to import this
> file from IDLE

I don't use Windows, so I can't tell you the specific Windows commands 
to use, but I'll tell you the general steps and you can convert them to 
Windows-specific instructions.

You need to:

(1) Create an environment variable called PYTHONPATH; 
(2) Set it to the path (or paths) you want;
(3) Ensure it gets automatically re-created each time you log in.
(4) When you restart Python, it will add the value of PYTHONPATH to your 
module search path.

Here is an example from Linux. I have this command in my .bashrc file:

export PYTHONPATH=/home/steve/python/

The "export" shell command creates the environment variable PYTHONPATH 
and sets it to "/home/steve/python/". By placing it in .bashrc, it will 
automatically be executed each time I log in or start a new shell.

Alternatively, you can use a .pth file. That's probably better if you 
have lots of directories to add. I've never done this, so take this 
with a grain of salt:

(1) Find the location of your Python 2.6 installation. This is probably 
something like C:\Applications\Python2.6.5\ or similar.

(2) Inside that directory should be another directory 
called "site-packages". It may or may not already have files or folders 
in it.

(3) Find, or create, a file called ".pth" inside site-packages. You may 
need to instruct Windows to show hidden files.

(4) Add the line:

C:\Users\Julius Hernandez\Desktop\Python2.6.5\Online Course\Chp.4\

to that file, optionally together with 

C:\Users\Julius Hernandez\Desktop\Python2.6.5\Online Course\Chp.1\
C:\Users\Julius Hernandez\Desktop\Python2.6.5\Online Course\Chp.2\
C:\Users\Julius Hernandez\Desktop\Python2.6.5\Online Course\Chp.3\


(5) Save and close the file, then restart Python.

> I am also having slight trouble understanding module packages as
> well, though I suspect it has something to do with not knowing how to
> configure PYTHONPATH or .pth files

A *module* is a single file containing Python code. Source code is 
name.py (also name.pyw on Windows only) while compiled byte code is 
name.pyc or name.pyo. There may be a few other extensions allowed on 
some other platforms.

A *package* is a directory containing multiple modules which make up a 
unified whole. Not every directory containing modules is a package -- 
you need a special file called __init__.py for Python to treat it as a 
package. (That's underscore underscore i n i t underscore underscore 
dot p y.)

If you have a directory structure like this:

+-- __init__.py
+-- beta.py
+-- gamma.py
+-- delta/
    +-- __init__.py
    +-- epsilon.py

then you have a package alpha containing two modules beta and gamma and 
one sub-package delta. Now put the whole thing (the alpha directory 
plus it's contents) somewhere in the Python path, and it will be 
visible to Python. Then you can do this:

import alpha

and then this:

x = alpha.delta.epsilon.some_function(1)
y = alpha.gamma.something(2)
z = alpha.something_else(3)

and so forth.

By the way, __init__.py doesn't have to contain anything, although it 
can contain code. It just needs to exist.

Steven D'Aprano

More information about the Tutor mailing list