Link to home
Start Free TrialLog in
Avatar of longjumps
longjumps

asked on

PYTHONPATH - collision to aviod

I have my application and path of it Python based.

I need variable PYTHONPATH to be set with my path, but don't want to overwrite antoher PYTHONPATH if it exists.

How can I overcome this and have my private PYTHONPATH?

Avatar of Papertrip
Papertrip
Flag of United States of America image

Which OS?
Avatar of sshah254
sshah254

On Unix

set $PYTHONPATH = $PYTHONPATH:your_own_python_path; export $PYTHONPATH

On DOS

set PYTHONPATH = %PYTHONPATH%;your_own_python_path

SS
Refer to this and let me know if any of it is not clear.

Unix

csh:
setenv PYTHONPATH = $PYTHONPATH:/your/own/path

Open in new window

bash:
export PYTHONPATH=$PYTHONPATH:/your/own/path

Open in new window

If you want to set it once for the user and make it permanent, echo the command into the shell rc script.  Any c shell can be substituted for csh, any bourne shell can be substituted for bash.

Windows
set PYTHONPATH=%PYTHONPATH%;x:\your\own\path

Open in new window

Avatar of longjumps

ASKER

Well, the original question is how can I set PYTHONPATH from inside of application, not touching environment variable.

Any recommendation here?
Avatar of gelonida
Hust to be sure Do you really want to change thPYTHONPATH environment variable from a script
(such, that subsequent scripts called with os.system or with sopprocess.popen inherit thius variable.
Or do  you just want to change the search path for the currently active executable?


In the first case you really have to change the environment variable of the currenbtly active python process and thus for all of its child processes in the other it is enough to chaneg sys.path.


The second approach inserts your desired search path first place to search for python modules.
In the example I assume, that you want to have the pythonpath set to the subdirectoryof  where your script is located.




import os, sys
# Determine the directory in which your script is located
script_path = os.path.dirname(__file__)

# now add your subdirectory, where your modules might be located.
my_python_path=os.path.join(script_path, 'mymodules')

# now convert path to an absolute path
my_python_path = os.path.abspath(my_python_path)

# now 'normalize' path
# not really required, buit results in more unified path names
my_python_path = os.path.normpath(my_python_path)


# Now change your 'pythonpath' for the current script only

sys.path.insert(0,my_python_path

# alternatively if you wanted, that your specific path is not searched first, but last you could do
sys.path.append(my_python_path

ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As gelonida wrote (he should get the points), there is a sys.path variable of the sys module.  It is a Python list of paths that is initialized also from PYTHONPATH environment variable -- see http://docs.python.org/tutorial/modules.html#the-module-search-path for deails.  After the initialization, Python uses only the sys.path internally to search for the modules.  Because of that this is the variable that you probably want to modify directly from inside your script.

You can print it to learn what is inside (here from Windows but it does not matter):

c:\tmp\_Python\longjumps\Q_27314014>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', 'C:\\Windows\\system32\\python27.zip', 'c:\\Python27\\DLLs', 'c:\\Python27\\lib', 'c:\\Python27\\lib\\plat-win', 'c:\\Python27\\lib\\lib-tk', 'c:\\Python27', 'c:\\Python27\\lib\\site-packages']
>>>

Open in new window

 

The Windows backslash is doubled -- you will see normal slashes in Unix. As the sys.path is a normal list, you can use .append() method to append the path to the end, or the .insert() method to insert your path elsewhere.

You should probably also describe your motivation.  It is not very usual to modify the sys.path or PYTHONPATH if the Python is installed correctly and if all modules were installed correctly.
Actually, I was pointing to the gelonida's http:#36556253 to be accepted ;)