41

I understand that sys.path refers to

  1. OS paths that have your system libraries. I take it that these refer to /lib in *nix or Windows on Windows.
  2. current directory python started from - I take it if Python is started from C:\Python, this would be the current path
  3. environmental variable $PYTHONPATH or %PYTHONPATH% - This refers to the path where I can call the Python binary from the command line
  4. you can add paths at runtime - Which I understand to be when I run IDLE

I am able to add paths by running the command sys.path.append however when I run the command sys.path.remove to 'delete' the path I appended, I am unable to do so. Is there a way to do so without having to close IDLE each time?

I am running Python 2.7 on Windows 7 as well as Ubuntu

PeanutsMonkey
  • 6,919
  • 23
  • 73
  • 103

3 Answers3

60

Everything works as intended on my machine :)

Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/sergey')
>>> sys.path
['', ..., '/home/sergey']
>>> sys.path.remove('/home/sergey')
>>> sys.path
['', ...]
>>> 

What exactly have you tried?

Regarding your understanding of things - I'm afraid there are some mis-understandings:

  1. sys.path is a list of directories which contain Python modules, not system libraries. So, simplifying, when you have something like import blah in your script, Python interpreter checks those directories one by one to check if there is a file called blah.py (or a subdirectory named blah with __init__.py file inside)

  2. Current directory is where the script is located, not where Python interpreter is. So if you have foo.py and bar.py in a directory, you can use import bar in foo.py and the module will be found because it's in the same directory.

  3. $PYTHONPATH is an environment variable which is getting appended to sys.path on interpreter startup. So, again, it is related to module search path and has nothing to do with starting Python from command line.

  4. Correct, you can modify sys.path at runtime - either when running a python script on in IDLE

See sys.path and site for more details.

Sergey
  • 11,892
  • 2
  • 41
  • 52
  • I ran the same exact command however it did not work the first time around i.e. I got an error that it could not be removed however after I closed IDLE and re-opened it, ran the command again, it works. Not sure why it didn't work the first time. Also is my understanding of `sys.path` correct? – PeanutsMonkey Dec 10 '12 at 02:14
  • This example works in Python IDLE Shell, but doesn't work in a Python Program (the directory is still present when I type `import sys` then `sys.path` in the Python IDLE Shell afterwards. I have to then do `sys.path.remove("example")` in the Python IDLE Shell to remove my example directory. Does anyone have a solution to this problem? – Edward Nov 05 '15 at 22:05
  • 1
    @Edward: the change to sys.path is not permanent and only affects the current process. If you want to make a change system-wide you'll need to have a look inside the site-packages directory and do some changes. See the lnk to the `site` module documentation I linked above – Sergey Nov 05 '15 at 23:28
  • @Sergey I understand what you are saying. So is `sys.path.append(item)` permanent then and do you know why there is Python support for permanent adding to `sys.path` but no support for permanently removing items from `sys.path`? – Edward Nov 06 '15 at 16:57
  • I have discovered that any edits made to `sys.path` in a program are definitely not permanent (when you restart the Python shell they disappear) and are only present in that version of `sys.path` in that program. What **Sergy** said is definitely right. – Edward Nov 06 '15 at 22:05
0

We can try below to insert, append or remove from sys.path

>>> import sys
>>>
>>> sys.path.insert(1, '/home/log')
>>> sys.path.append('/home/log')
>>> sys.path
['', '/home/log']
>>> sys.path.remove('/home/log')
>>> sys.path
>>> ['']
>>>
Krishn Singh
  • 81
  • 1
  • 2
-3

Use

sys.path.append('path/to/file')

instead of

sys.path.append('path/to/file/')

Same with sys.path.remove().

username
  • 61
  • 7