Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PYTHONPATH - collision to aviod

Posted on 2011-09-18
9
Medium Priority
?
552 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 17

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 17

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 17

Accepted Solution

by:
gelonida earned 2000 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 29

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 29

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 how to create, access, and change arrays in the C programming language.
Suggested Courses

636 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