Solved

PYTHONPATH - collision to aviod

Posted on 2011-09-18
9
538 Views
Last Modified: 2012-05-12
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?

0
Comment
Question by:longjumps
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 21

Expert Comment

by:Papertrip
ID: 36555644
Which OS?
0
 
LVL 9

Expert Comment

by:sshah254
ID: 36555657
On Unix

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

On DOS

set PYTHONPATH = %PYTHONPATH%;your_own_python_path

SS
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36555721
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

0
 
LVL 1

Author Comment

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

Any recommendation here?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:gelonida
ID: 36556232
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.
0
 
LVL 16

Expert Comment

by:gelonida
ID: 36556253


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

0
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
ID: 36556270
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:

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

if running_on_windows:
   quote_character = '"'
   separator = ';'
else:
    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
        return
    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

0
 
LVL 28

Expert Comment

by:pepr
ID: 36556840
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.
0
 
LVL 28

Expert Comment

by:pepr
ID: 36562865
Actually, I was pointing to the gelonida's http:#36556253 to be accepted ;)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now