I understand that sys.path
refers to
/lib
in *nix or Windows
on Windows.C:\Python
, this would be the current pathI 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
os. remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.
Introduction to Python module search path Python will search for the module.py file from the following sources: The current folder from which the program executes. A list of folders specified in the PYTHONPATH environment variable, if you set it before.
The easiest way to change it is to add a file /usr/local/lib/python2. 6/dist-packages/site-packages. pth containing ../site-packages . Alternatively, maybe you can teach the package to use site.
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:
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)
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.
$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 IDLE
See sys.path and site for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With