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
5
votes
1 answer

Why does PYTHONPATH with trailing colon add current directory to sys.path?

Consider a Python project like this: foo/ __init__.py scripts/ run.py demo.sh Under normal circumstances, attempting to import from the foo package will fail if you run the script from the root of the project, because default…
FMc
  • 39,513
  • 12
  • 72
  • 131
5
votes
0 answers

Why is my Python 'sys.path' different for each tool I use to run my code?

I'm confused about the rules used to construct my Python sys.path. I understand that the fist entry will be "the directory containing the script that was used to invoke the Python interpreter", or an empty string if Python was invoked interactively,…
orome
  • 35,904
  • 38
  • 156
  • 345
4
votes
1 answer

How to permanently remove value from sys.path?

for some reason, ' ' value (empty value) was added to my sys.path array and I can't run 'setup.py install'. I can't find a way to remove it permanently. I did it through python shell: sys.path.remove('') and it removes it until closing the shell. I…
user2880391
  • 2,241
  • 6
  • 26
  • 59
4
votes
2 answers

Twisted trial, PYTHONPATH and sys.path

I'm trying to reproduce example from "Test-driven development with Twisted" at http://twistedmatrix.com/documents/current/core/howto/trial.html. I created…
4
votes
3 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
  • 161
  • 1
  • 3
  • 6
4
votes
0 answers

Wrong sys.path in jupyter notebook when launched in a virtual environment

I created a virtual environment named sampleenv, however when I launched jupyter notebook inside sampleenv, I noticed that it is referencing/siting python packages from my another virtual environment named openfaceenv. Here's the output when calling…
bninopaul
  • 2,039
  • 2
  • 11
  • 20
4
votes
2 answers

importing beautiful soup in python3

I am trying to import beautifulsoup in python3. I have this line: from bs4 import BeautifulSoup It says: from bs4 import BeautifulSoup File "/usr/local/lib/python2.7/dist-packages/bs4/__init__.py", line 175 except Exception, e: …
nishantsingh
  • 4,017
  • 4
  • 21
  • 44
4
votes
1 answer

setting import module path in Jython - strange behavior

I'm building Java to Jython bridge class. The task I'm trying to solve is to make Jython to look for python modules in my application working directory (also known as program execution directory). I'm doing so by appending…
vvladymyrov
  • 5,177
  • 1
  • 28
  • 45
4
votes
1 answer

Restrict import to specific path, without restricting the imported module's path

Is it possible to import a module from a specific directory, without affecting the import path of the imported module? If I was to temporarily replace sys.path with the desired directory, the imported module would not be able to import anything…
Acorn
  • 44,010
  • 23
  • 124
  • 163
3
votes
2 answers

dist-packages and sys.path

I have some code inside myapplication that look for some files inside myapplications' directories. I'm working with AptanaStudio, and I see that my code run OK, but when I create the debian package and I install it in another computer the search is…
Antonio
  • 61
  • 1
  • 7
3
votes
2 answers

python3 cannot find a module that I could import with python2

I'm trying to switch from python2 to python3. In the process I'm also switching from anaconda to miniconda3 as my primary package management tool. There are some other packages that I clone from github. I found that I can no longer import any…
3
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
  • 901
  • 8
  • 31
3
votes
2 answers

Can I have my pip user-installed package be preferred over system?

I would like to figure out a "fool-proof" installation instruction to put in the README of a Python project, call it footools, such that other people in our group can install the newest SVN version of it on their laptops and their server…
unhammer
  • 3,461
  • 2
  • 29
  • 44
3
votes
1 answer

Differences in sys.path when python2.7 is invoked normally or via subprocess

Question Why is it that python2.7 when called using a subprocess via python3 does not have the same sys.path as python2.7 called normally? Specifically, python2.7 via subprocess does not have the "/path/to/site-packages/" directory in…
pedmiston
  • 199
  • 2
  • 11
3
votes
2 answers

prevent python from loading a pth file

My situation is as follows: I have a locally installed version of python. There exists also a global one, badly installed, which I do not want to use. (I don't have admin priviliges). On /usr/local/lib/site-packages there is a x.pth file containing…
Korem
  • 9,501
  • 5
  • 46
  • 67
1
2
3
9 10