Solved

PYTHONPATH - collision to aviod

Posted on 2011-09-18
9
543 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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.

809 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