Reason for not allowing import twice but allowing reload()

Steven D'Aprano steve at pearwood.info
Wed Mar 9 08:53:06 EST 2016


Sorry for the delay in answering!

On Sat, 5 Mar 2016 11:51 pm, alien2utoo at gmail.com wrote:

> Steven,
> 
>> There are better ways to manage your Python path than to manually insert
>> paths into sys.path like that. What version of Python are you using?
> 
> I would love to know, apart from PYTHONPATH and sys.path.append() route.
> 
> I am using Python 2.7.11 to start with as suggested by my employer.

Python creates sys.path from four different sources:

(1) The directory of the script you are running, if you are running a
script, otherwise the current directory.

(2) Any paths listed in the PYTHONPATH environment variable.

(3) A small number of fixed paths, which are built into the interpreter.

(4) Finally the site.py module does extensive customization of sys.path.

If you have just one or two paths to add, PYTHONPATH is the easiest and most
convenient. But for more extensive additions to the path, number (4) is the
way to go. It gives you many options!

Let's suppose I want to add two locations to my path alone, not for any
other users:

/home/loretta/python
/home/loretta/scripts


Here are four methods for adding user-specific locations to sys.path, plus
two more for adding system-wide locations that apply to all users:



Method 1: PYTHONPATH environment variable
=========================================

In my ~/.bashrc file, I put this line:

export PYTHONPATH="/home/loretta/python:/home/loretta/scripts"



Method 2: startup file
======================

(This method only works when I run the interactive interpreter, not when
running scripts.)

In my ~/.bashrc file, I put this line:

export PYTHONSTARTUP="/home/loretta/startup.py"


Then I edit startup.py to include any python code I like:

print "Running Python startup file..."
import sys
sys.path.append('/home/loretta/python')
sys.path.append('/home/loretta/scripts')
print "Done!"



Method 3: usercustomize.py
==========================

I create a file:

~/.local/lib/python2.7/site-packages/usercustomize.py


which can contain any Python code I like. Import sys and add to the path:

import sys
sys.path.append('/home/loretta/python')
sys.path.append('/home/loretta/scripts')



Method 4: user .pth file
========================

I create a file containing the paths I want to add, one path per line:


/home/loretta/python
/home/loretta/scripts


Blank lines and lines starting with # are ignored.

I save this to:

~/.local/lib/python2.7/site-packages/loretta.pth

I can have as many .pth files as I like, and name them anything I like, so
long as the names end with .pth and they are in my local site packages
directory.



Method 5: sitecustomize.py
==========================

This is like usercustomize.py (method 3), except it applies to all users.

Create a file sitecustomize.py, and put it in Python's standard library,
which is usually found here:

/usr/local/lib/python2.7/

You will need to be root to do this.


Method 6: site .pth file
========================

This is like method 4, except it applies to all users.

Create a .pth file containing the paths you want added, and put it in
Python's standard library. You will need to be the root user to do this.



-- 
Steven




More information about the Python-list mailing list