Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing path from Python search module path

Tags:

python-2.7

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

like image 411
PeanutsMonkey Avatar asked Dec 10 '12 01:12

PeanutsMonkey


People also ask

How do I remove a path in Python?

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.

What is Python module search path?

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.

How do I change the path of a Python site package?

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.


1 Answers

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.

like image 141
Sergey Avatar answered Sep 26 '22 21:09

Sergey