Questions tagged [sys.path]

An automatically initialized list in Python that contains the search path for modules.

In Python, sys.path is a list of the runtime locations searched when importing a module.

From the Python documentation:

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

A program is free to modify this list for its own purposes.

143 questions
111
votes
3 answers

Permanently adding a file path to sys.path in Python

I had a file called example_file.py, which I wanted to use from various other files, so I decided to add example_file.py to sys.path and import this file in another file to use the file. To do so, I ran the following in IPython. import…
Shiva Krishna Bavandla
  • 20,872
  • 61
  • 174
  • 298
97
votes
6 answers

PYTHONPATH vs. sys.path

Another developer and I disagree about whether PYTHONPATH or sys.path should be used to allow Python to find a Python package in a user (e.g., development) directory. We have a Python project with a typical directory structure: Project setup.py …
gaefan
  • 14,322
  • 16
  • 52
  • 100
65
votes
9 answers

Get parent of current directory from Python script

I want to get the parent of current directory from Python script. For example I launch the script from /home/kristina/desire-directory/scripts the desire path in this case is /home/kristina/desire-directory I know sys.path[0] from sys. But I don't…
Fedorenko Kristina
  • 2,017
  • 2
  • 15
  • 17
30
votes
4 answers

Python: select one of multiple installed module versions

On my system, I have several modules installed multiple times. To give an example, numpy 1.6.1 is installed in the standard path at /usr/lib/python2.7/dist-packages, and I have an updated version of numpy 1.8.0 installed at…
Jenny
  • 557
  • 1
  • 6
  • 10
14
votes
6 answers

Add a directory to Python sys.path so that it's included each time I use Python

Currently, when trying to reference some library code, I'm doing this at the top of my python file: import sys sys.path.append('''C:\code\my-library''') from my-library import my-library Then, my-library will be part of sys.path for as long as the…
Ben McCormack
  • 29,788
  • 45
  • 138
  • 211
14
votes
2 answers

How to refresh sys.path?

I've installed some packages during the execution of my script as a user. Those packages were the first user packages, so python didn't add ~/.local/lib/python2.7/site-packages to the sys.path before script run. I want to import those installed…
rominf
  • 2,123
  • 2
  • 18
  • 31
12
votes
1 answer

virtualenv using incorrect sys.path

Things were working fine a moment ago. I have no idea what I did to piss off virtualenv, but it's acting very strangely now. Any help is appreciated. When making a virtualenv, I use this command: virtualenv -p /usr/bin/python3 venv Now I see that…
Tom A
  • 381
  • 2
  • 9
11
votes
4 answers

Python: ensure os.environ and sys.path are equal: web-requests, shell, cron, celery

I want to ensure that os.environ and sys.path are identical for all ways we start the Python interpreter: web requests via Django, and Apache mod_wsgi Cron jobs Interactive logins via ssh Celery jobs Jobs started via systemd Is there a common way…
guettli
  • 26,461
  • 53
  • 224
  • 476
10
votes
2 answers

How do you modify sys.path in Google App Engine (Python)?

I've tried adding the following line to my handler script (main.py), but it doesn't seem to work: sys.path.append('subdir') subdir lives in the my root directory (i.e. the one containing app.yaml). This doesn't seem to work, because when I try to…
allyourcode
  • 19,438
  • 17
  • 73
  • 100
9
votes
3 answers

Setting a default sys.path for a Notebook

I have all my .py files inside a folder script and all my IPython-notebooks under a folder named Notebook. There are multiple cross dependencies for each notebook file on one or more files on script. Having sys.path.append on top of every notebook…
Sreejith Menon
  • 927
  • 1
  • 15
  • 26
9
votes
1 answer

python: Interplay between lib/site-packages/site.py and lib/site.py

Due to a specific problem which I managed to solve, I spent most of today figuring out how site.py(s) work. There is a point which I don't understand. As far as I understand, when python is loaded, first lib/python2.7/site-packages/site.py is run.…
Korem
  • 9,501
  • 5
  • 46
  • 67
9
votes
2 answers

Why python finds module instead of package if they have the same name?

Here is my directory structure: /home/dmugtasimov/tmp/name-res root tests __init__.py test_1.py __init__.py classes.py extra.py root.py File contents: root/tests/_init_.py import…
Dmitry Mugtasimov
  • 3,483
  • 2
  • 13
  • 22
7
votes
2 answers

How is the python module search path determined on Mac OS X?

When a non built-in module is imported, the interpreter searches in the locations given by sys.path. sys.path is initialized from these locations (http://docs.python.org/library/sys.html#sys.path): the directory containing the input script (or the…
christianbrodbeck
  • 1,675
  • 2
  • 15
  • 22
5
votes
2 answers

Application-specific PYTHONPATH

I have an application with a heirarchy of packages. There are a fair number of modules that reference other modules higher up in the package heirarchy. As exemplified below, I can use relative imports to solve this problem. However, running the…
MikeWyatt
  • 7,692
  • 10
  • 44
  • 69
5
votes
2 answers

Are there more search paths than in sys.path?

I thought that the sys.path was a complete list of all search paths for Python modules. However, on my Ubuntu machine, '/usr/local/lib/python2.6/dist-packages/' is where almost all my modules are and that path is not in sys.path. And I can still…
Lucy Brennan
  • 555
  • 1
  • 7
  • 13
1
2 3
9 10