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?

Who is Participating?

Improve company productivity with a Business Account.Sign Up

gelonidaConnect With a Mentor Commented:
If you really wanted to modify the pythonpath variable in the script, then you had to make a distinction between the way a path is stored on windows ';' as separator and on windows / linux /etc.

I don't know of a ready made function for this, though it might exist and I just don't know it.

Now you could just use:

import os, sys
# determine on which OS you're running
running_on_windows = sys.platform == 'win32'

if running_on_windows:
   quote_character = '"'
   separator = ';'
    quote_character = ''
    separator = ':'

def insert_to_pythonpath(path_name):
    if not 'PYTHONPATH' in os.environ:
        # it seems PYTHONPATH was empty. so just set it
        os.environ['PYTHONPATH'] = path_name
    quoted_path  = quote_character + path_name + quote_character
    new_pythonpath = quoted_path + separator + os.environ['PYTHONPATH']
    os.environ['PYTHONPATH']  =  new_pythonpath

Open in new window

Which OS?
On Unix

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


set PYTHONPATH = %PYTHONPATH%;your_own_python_path

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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


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

Open in new window

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.

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

Open in new window

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

Any recommendation here?
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


# alternatively if you wanted, that your specific path is not searched first, but last you could do

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 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):

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\\', '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 ;)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.