how to change sys.path?

B

bruno at modulix

Ju said:
is python search module by paths in sys.path?

sys.path is the list of path where the Python interpreter will search
modules, yes.
how to change it manuallly?

"manually" ?-)

You mean "dynamically, by code" ? If yes, it's just a list. You can
modify it like you'd do for any other list.

Else, you may want to look at the PYTHON_PATH environnement variable.
 
J

Ju Hui

yes, I mean I want change the sys.path value and save it for next
using.
I can change the value of sys.path, but I can't "save" it permanently.
There is no python_path environment on my pc, what the relationship
between it and the sys.path?
 
J

John Salerno

Ju said:
yes, I mean I want change the sys.path value and save it for next
using.
I can change the value of sys.path, but I can't "save" it permanently.
There is no python_path environment on my pc, what the relationship
between it and the sys.path?

In Windows, at least, you can create the PYTHONPATH variable and assign
to it the paths of the directories you want Python to check for when
running a script.
 
J

Jarek Zgoda

John Salerno napisa³(a):
In Windows, at least, you can create the PYTHONPATH variable and assign
to it the paths of the directories you want Python to check for when
running a script.

Setting Windows envvar by changing os.environ dict will work only for
processes running in the same environment (i.e. in the same shell session).
 
J

John Salerno

Jarek said:
John Salerno napisa³(a):


Setting Windows envvar by changing os.environ dict will work only for
processes running in the same environment (i.e. in the same shell session).

I meant actually adding the PYTHONPATH variable to the environment
variables list.
 
B

Bruno Desthuilliers

Ju Hui a écrit :
yes, I mean I want change the sys.path value and save it for next
using.
>
I can change the value of sys.path, but I can't "save" it permanently. No.
There is no python_path environment on my pc,

sorry, I meant PYTHONPATH (all caps, no underscore). Like all
environnement variable, it can be set up.
what the relationship
between it and the sys.path?

Don't you guess ?
 
J

Ju Hui

yes, we can change PYTHONPATH to add some path to sys.path value, but
how to remove item from sys.path?
 
B

Ben Finney

[Please provide some context when you respond in an existing
discussion, by quoting the original message and removing any parts
irrelevant to your reply.]

Ju Hui said:
yes, we can change PYTHONPATH to add some path to sys.path value, but
how to remove item from sys.path?

As pointed out already, sys.path is a list, which can be manipulated
like any other list.

<URL:http://docs.python.org/lib/typesseq-mutable.html>
 
S

Steve Holden

Ju said:
yes, we can change PYTHONPATH to add some path to sys.path value, but
how to remove item from sys.path?
That would be

del sys.path[3]

for example. Of course you may need to search sys.path to determine the
exact element you need to delete, but sys.path is just like any other
list in Python and can (as many people have already said in this thread)
be manipulated just like al the others.

regards
Steve
 
J

John Salerno

Dennis said:
You're looking at editing the Windows registry for that...

I just right-clicked on My Computer --> Properties --> Advanced -->
Environment Variables, and added a new one called PYTHONPATH. I don't
know if that edits the registry, but you don't *manually* have to edit
the registry if you do it that way...unless of course you aren't
supposed to be doing it that way! But it worked anyway. :)
 
D

Dennis Lee Bieber

I just right-clicked on My Computer --> Properties --> Advanced -->
Environment Variables, and added a new one called PYTHONPATH. I don't
know if that edits the registry, but you don't *manually* have to edit
the registry if you do it that way...unless of course you aren't
supposed to be doing it that way! But it worked anyway. :)

I may have gotten slightly confused -- I had an impression that, at
least one poster in the thread, wanted to do this from within a Python
program. That does go into registry modifications.

For example, look at the entries under:

(system environment, I believe)
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment

(user specific environment)
HKEY_CURRENT_USER\Environment

Though in the case of PYTHONPATH, the core value seems to be in (for
my install)

HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.3\PythonPath

which also has subkeys for Pythonwin, win32, and win32com

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
A

Andrew Robert

Dennis said:
I may have gotten slightly confused -- I had an impression that, at
least one poster in the thread, wanted to do this from within a Python
program. That does go into registry modifications.

For example, look at the entries under:

(system environment, I believe)
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment

(user specific environment)
HKEY_CURRENT_USER\Environment

Though in the case of PYTHONPATH, the core value seems to be in (for
my install)

HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.3\PythonPath

which also has subkeys for Pythonwin, win32, and win32com

Instead of messing with the registry, wouldn't it be easier to just add
a line similar to this within your code?

sys.path.append(r'\\mynetwork\share')

I use something similar so that all my scripts can locate the same
home-grown modules no matter where they are run from.
 
J

Ju Hui

yes, we can add path to PYTHONPATH,but how to remove some items?
my sys.path:.... print x
....

D:\usr\local\lib\site-packages\setuptools-0.6a11-py2.4.egg
D:\usr\local\lib\site-packages\clientcookie-1.3.0-py2.4.egg
c:\temp
C:\WINDOWS\system32\python24.zip
C:\Documents and Settings\phpbird
D:\usr\local\DLLs
D:\usr\local\lib
D:\usr\local\lib\plat-win
D:\usr\local\lib\lib-tk
D:\usr\local
D:\usr\local\lib\site-packages
the value of
HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.4\PythonPath
D:\usr\local\Lib;D:\usr\local\DLLs;D:\usr\local\Lib\lib-tk;

the content are difference, how to remove
C:\WINDOWS\system32\python24.zip
or
D:\usr\local\lib\site-packages\clientcookie-1.3.0-py2.4.egg
?

thanks.
 
J

jkgunjal

sys.path.append("<path containing module to be imported>")

eg. sys.path.append("/home/webdoc")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top