how to change sys.path?

Discussion in 'Python' started by Ju Hui, May 23, 2006.

  1. Ju Hui

    Ju Hui Guest

    is python search module by paths in sys.path?
    how to change it manuallly?
    Ju Hui, May 23, 2006
    #1
    1. Advertising

  2. 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 ''.split('@')])"
    bruno at modulix, May 23, 2006
    #2
    1. Advertising

  3. Ju Hui

    Ju Hui Guest

    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?
    Ju Hui, May 23, 2006
    #3
  4. Ju Hui

    John Salerno Guest

    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.
    John Salerno, May 23, 2006
    #4
  5. Ju Hui

    Jarek Zgoda Guest

    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/
    Jarek Zgoda, May 23, 2006
    #5
  6. Ju Hui

    John Salerno Guest

    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.
    John Salerno, May 23, 2006
    #6
  7. Ju Hui

    Ben Finney Guest

    Bruno Desthuilliers <> 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
    Ben Finney, May 23, 2006
    #7
  8. 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 ?
    Bruno Desthuilliers, May 24, 2006
    #8
  9. Ju Hui

    Ju Hui Guest

    yes, we can change PYTHONPATH to add some path to sys.path value, but
    how to remove item from sys.path?
    Ju Hui, May 24, 2006
    #9
  10. On Tue, 23 May 2006 20:21:10 GMT, John Salerno
    <> 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...
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, May 24, 2006
    #10
  11. Ju Hui

    Ben Finney Guest

    [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" <> 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
    Ben Finney, May 24, 2006
    #11
  12. Ju Hui

    Steve Holden Guest

    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
    Steve Holden, May 24, 2006
    #12
  13. Ju Hui

    John Salerno Guest

    Dennis Lee Bieber wrote:
    > On Tue, 23 May 2006 20:21:10 GMT, John Salerno
    > <> 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. :)
    John Salerno, May 24, 2006
    #13
  14. On Wed, 24 May 2006 14:45:55 GMT, John Salerno
    <> 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\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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, May 24, 2006
    #14
  15. Dennis Lee Bieber wrote:
    > On Wed, 24 May 2006 14:45:55 GMT, John Salerno
    > <> 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\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.
    Andrew Robert, May 24, 2006
    #15
  16. Ju Hui

    John Salerno Guest

    Dennis Lee Bieber wrote:

    > I may have gotten slightly confused


    That's my job. :)
    John Salerno, May 24, 2006
    #16
  17. Ju Hui

    Ju Hui Guest

    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\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.
    Ju Hui, May 25, 2006
    #17
  18. Ju Hui

    Guest

    sys.append(<write path of the module u r importing>)

    Bye.
    , May 25, 2006
    #18
  19. Ju Hui

    Guest

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

    eg. sys.path.append("/home/webdoc")
    , May 25, 2006
    #19
  20. On Wed, 24 May 2006 17:24:08 GMT, John Salerno
    <> 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>
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, May 25, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronald Meier
    Replies:
    2
    Views:
    1,471
    Keith S.
    Oct 21, 2003
  2. Jeff Wagner

    sys.path.append('/my/new/path')

    Jeff Wagner, Nov 24, 2003, in forum: Python
    Replies:
    3
    Views:
    519
    Patrick Useldinger
    Nov 24, 2003
  3. Replies:
    3
    Views:
    386
  4. wen
    Replies:
    10
    Views:
    961
    Peter Hansen
    Aug 19, 2005
  5. Jeremy Nicoll - news posts

    Import, how to change sys.path on Windows, and module naming?

    Jeremy Nicoll - news posts, Mar 1, 2008, in forum: Python
    Replies:
    7
    Views:
    683
    Gabriel Genellina
    Mar 4, 2008
Loading...

Share This Page