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.

169 questions
145
votes
4 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
  • 25,548
  • 75
  • 193
  • 313
113
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 …
new name
  • 15,861
  • 19
  • 68
  • 114
95
votes
10 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,607
  • 2
  • 19
  • 18
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
  • 527
  • 1
  • 6
  • 11
19
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,719
  • 3
  • 21
  • 39
18
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
  • 32,086
  • 48
  • 148
  • 223
13
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
  • 401
  • 4
  • 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
  • 25,042
  • 81
  • 346
  • 663
11
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
  • 11,383
  • 7
  • 55
  • 72
10
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
  • 1,057
  • 1
  • 18
  • 27
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
  • 21,871
  • 18
  • 78
  • 106
10
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,858
  • 2
  • 18
  • 26
7
votes
1 answer

Sys.path.insert inserts path to module but imports are not working

I want to import a module in a project and it gives me lots of troubles because of an import error. So I decided to write a little test to see where the problem lies. I add a folder to my sys path and try to import it. And I get an Import Error: no…
Micromegas
  • 1,499
  • 2
  • 20
  • 49
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
  • 2,113
  • 2
  • 19
  • 24
6
votes
4 answers

How to undo sys.path.append(pathToModule)

I have been trying to fix the python path on my cpu, and I originally was just trying to change my .bash_profile, but that wasn't working, so I used import sys sys.pat.append(path/To/Module) and now when I run my script, I get this error…
SUPhys
  • 221
  • 1
  • 5
  • 8
1
2 3
11 12