469,631 Members | 1,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

how to change sys.path?

is python search module by paths in sys.path?
how to change it manuallly?

May 23 '06 #1
19 39461
Ju Hui wrote:
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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 23 '06 #2
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?

May 23 '06 #3
Ju Hui wrote:
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.
May 23 '06 #4
John Salerno napisa³(a):
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.


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

--
Jarek Zgoda
http://jpa.berlios.de/
May 23 '06 #5
Jarek Zgoda wrote:
John Salerno napisa³(a):
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.


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.
May 23 '06 #6
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 ?

May 23 '06 #7
Bruno Desthuilliers <bd*****************@free.quelquepart.fr> writes:
Ju Hui a écrit :
what the relationship between it and the sys.path?


Don't you guess ?


He's following sound Python philosophy :-)

--
\ Hercules Grytpype-Thynne: "Well, Neddie, I'm going to be |
`\ frank." Ned Seagoon: "Right, I'll be Tom." Count Moriarty: |
_o__) "I'll be Gladys." *slap* -- The Goon Show, _World War I_ |
Ben Finney

May 23 '06 #8
yes, we can change PYTHONPATH to add some path to sys.path value, but
how to remove item from sys.path?

May 24 '06 #9
[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" <ph*****@gmail.com> writes:
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>

--
\ "I have never made but one prayer to God, a very short one: 'O |
`\ Lord, make my enemies ridiculous!' And God granted it." -- |
_o__) Voltaire |
Ben Finney

May 24 '06 #10
Ju Hui wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

May 24 '06 #11
Dennis Lee Bieber wrote:
On Tue, 23 May 2006 20:21:10 GMT, John Salerno
<jo******@NOSPAMgmail.com> declaimed the following in comp.lang.python:
I meant actually adding the PYTHONPATH variable to the environment
variables list.


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. :)
May 24 '06 #12
Dennis Lee Bieber wrote:
On Wed, 24 May 2006 14:45:55 GMT, John Salerno
<jo******@NOSPAMgmail.com> declaimed the following in comp.lang.python:
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\Contro l\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.
May 24 '06 #13
Dennis Lee Bieber wrote:
I may have gotten slightly confused


That's my job. :)
May 24 '06 #14
yes, we can add path to PYTHONPATH,but how to remove some items?
my sys.path:
import sys
for x in 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\Li b\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.

May 25 '06 #15
sys.append(<write path of the module u r importing>)

Bye.

May 25 '06 #16
sys.path.append("<path containing module to be imported>")

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

May 25 '06 #17
also se topic named
'problem(s) with import from parent dir: "from ../brave.py import
sir_robin" '

I use this every day now:
sys.path.append("../../py_scripts")

best wishes,
Per

May 25 '06 #18
Michael Yanowitz wrote:
Is there something like a .pythoninitrc which can run whenever we start
Python
that can load a file with many sys.path.append(), etc?
If not is there some way to modify the Python shell constructor and
destructor?

Thanks in advance:
Michael yanowitz


Yes, there is the user module:
http://docs.python.org/lib/module-user.html
which you have to explicitly import and which will look for
..pythonrc.py file in user's home directory and execute it.

The other option is a sitecustomize module, which should
be put somewhere on the initial search path. It will be
imported automatically during the interpreter initialization.
See:
http://docs.python.org/lib/module-site.html
for details.

Ziga

May 25 '06 #19
Dennis Lee Bieber wrote:
On Wed, 24 May 2006 17:24:08 GMT, John Salerno
<jo******@NOSPAMgmail.com> declaimed the following in comp.lang.python:
Dennis Lee Bieber wrote:
I may have gotten slightly confused

That's my job. :)


Okay:

You have gotten me slightly confused <G>


Your job is to pay no attention to me and my blathering. :)
May 25 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Peter Vestergaard | last post: by
5 posts views Thread by sling blade | last post: by
10 posts views Thread by Hugh | last post: by
reply views Thread by Jurgen Oerlemans | last post: by
10 posts views Thread by mukeshhtrivedi | last post: by
13 posts views Thread by lawpoop | last post: by
5 posts views Thread by waqasahmed996 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.