85

My code is:

import scriptlib.abc
import scriptlib.xyz
def foo():
 ... some operations

but the scriptlib is in some other directory, so I will have to include that directory in environment variable "PYTHONPATH".

Is there anyway in which I can first add the scriptlib directory in environment variable "PYTHONPATH" before import statement getting executed like:

import sys
sys.path.append('/mypath/scriptlib')
import scriptlib.abc
import scriptlib.xyz
def foo():
 ... some operations

If so, is the value only for that command prompt or is it global?

starball
59.9k53 gold badges323 silver badges1k bronze badges
asked Feb 27, 2013 at 10:20
1
  • 2
    Did you try it? That code should just work. The new value of sys.path is however local to your program (the $PYTHONPATH variable is only read when starting the interpreter to fill sys.path, it is not written back to the environment). Commented Feb 27, 2013 at 10:23

2 Answers 2

95

This will add a path to your Python process / instance (i.e. the running executable). The path will not be modified for any other Python processes. Another running Python program will not have its path modified, and if you exit your program and run again the path will not include what you added before. What are you are doing is generally correct.

set.py:

import sys
sys.path.append("/tmp/TEST")

loop.py

import sys
import time
while True:
 print sys.path
 time.sleep(1)

run: python loop.py &

This will run loop.py, connected to your STDOUT, and it will continue to run in the background. You can then run python set.py. Each has a different set of environment variables. Observe that the output from loop.py does not change because set.py does not change loop.py's environment.

A note on importing

Python imports are dynamic, like the rest of the language. There is no static linking going on. The import is an executable line, just like sys.path.append....

answered Feb 27, 2013 at 10:24
Sign up to request clarification or add additional context in comments.

2 Comments

but i don't want the path in my code. I want my code to be clean and portable. is there no concept of a "python project". (Say for Visual-studio-code or for Atom.io)?!
Yes, the equivalent of a Python "project" is VirtualEnv. VirtualEnvWrapper is a nice interface to use. The difference between this and Visual Studio is that there is already a global Python environment, and virtualenv just makes a virtual layer on top. There may be other tools, I've been away from Python for a few years.
9

As also noted in the docs here.

Go to Python X.Y/Lib (Windows) or /usr/lib/pythonX.Y (Linux) and add these lines to the site.py there,

import sys
sys.path.append("yourpathstring")

This changes your sys.path so that on every load, it will have that value in it..

As stated here about site.py,

This module is automatically imported during initialization. Importing this module will append site-specific paths to the module search path and add a few builtins.

For other possible methods of adding some path to sys.path see these docs

answered Feb 27, 2013 at 10:36

5 Comments

but this is same as setting the value in environment variables manually. What I need is to change the path of library quite frequently like getting the scriptlib path from a file and setting it before using them.
No, this value is appended in every run of python.exe as site.py is automatically loaded during initialization.. So, you don't need the import sys sys.path.append.... in each file.
consider a situation where I have 2 copies of same python script. one script wants to get library from C:\testers\scriptlib and other wants from D:\developer\scriptlib. In that case how can site.py determine to assign which path & to whom.
@DKG In that situation, How is Python supposed to know which file you're referring to? You simply shouldn't name 2 files the same...
Ah! The door doesn't open because the silly keyhole is wrong!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.