tags:

views:

19

answers:

2
import sys
print sys.path

sys.path+=['D:\\zjm_code']
print sys.path

it can't be save,how does do it,

thanks

A: 

you can use append()

import sys
sys.path.append("/mypath")

to save it permanently, you can export PYTHONPATH in the user shell initialization file (eg .bash_profile)

PYTHONPATH=/path/to/module/directory:/path/to/module2/dir2

on Windows systems, you can follow instructions here.

ghostdog74
this can't save also.
zjm1126
Indeed, @zjm, it's just a simpler way to do _exactly_ the same job as your `+=`.
Alex Martelli
+2  A: 

Where is environment variable PYTHONPATH defined in your working environment?

In Unix-like systems it would be in a bash script such as ~/.bashrc.

In Windows it could be a .BAT or .CMD file but more often will be in the registry.

"Saving" a setting of PYTHONPATH to a file is easy.

Writing to the Windows registry is much harder, alas. Doable, yes, but, if you get it in the least wrong, you're likely to make your whole machine unusable.

In your site-packages directory (which should itself be in sys.path) you can create a file named sitecustomize.py that is automatically imported, if it exists, each time a Python program starts. I suggest that this is simpler and less risky than messing with the registry. So, write your sys.path manipulations to such a sitecustomize.py file.

Alex Martelli