I understand that sys.path
refers to
- OS paths that have your system libraries. I take it that these refer to
/lib
in *nix orWindows
on Windows. - current directory python started from - I take it if Python is started from
C:\Python
, this would be the current path - environmental variable $PYTHONPATH or %PYTHONPATH% - This refers to the path where I can call the Python binary from the command line
- 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
We can try below to insert, append or remove from sys.path
Everything works as intended on my machine :)
What exactly have you tried?
Regarding your understanding of things - I'm afraid there are some mis-understandings:
sys.path
is a list of directories which contain Python modules, not system libraries. So, simplifying, when you have something likeimport blah
in your script, Python interpreter checks those directories one by one to check if there is a file calledblah.py
(or a subdirectory namedblah
with__init__.py
file inside)Current directory is where the script is located, not where Python interpreter is. So if you have
foo.py
andbar.py
in a directory, you can useimport bar
infoo.py
and the module will be found because it's in the same directory.$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.Correct, you can modify
sys.path
at runtime - either when running a python script on in IDLESee sys.path and site for more details.