178

I am trying to import a module from a particular directory.

The problem is that if I use sys.path.append(mod_directory) to append the path and then open the python interpreter, the directory mod_directory gets added to the end of the list sys.path. If I export the PYTHONPATH variable before opening the python interpreter, the directory gets added to the start of the list. In the latter case I can import the module but in the former, I cannot.

Can somebody explain why this is happening and give me a solution to add the mod_directory to the start, inside a python script ?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
UnadulteratedImagination
  • 1,971
  • 2
  • 13
  • 15

5 Answers5

269

This is working as documented. Any paths specified in PYTHONPATH are documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append() appends to the existing path. See here and here. If you want a particular directory to come first, simply insert it at the head of sys.path:

import sys
sys.path.insert(0,'/path/to/mod_directory')

That said, there are usually better ways to manage imports than either using PYTHONPATH or manipulating sys.path directly. See, for example, the answers to this question.

Community
  • 1
  • 1
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • 1
    thanks for the reply. The problem was that I didn't realize that to add module packaged as a .egg file you have to include the filename instead of just the directory in python 2.6 – UnadulteratedImagination Apr 20 '13 at 23:41
  • 2
    Hey man, this broke my django. Are you sure you mean to tell people to put it at the top?!?!!? – R Claven Oct 12 '14 at 05:50
  • 22
    @RClaven, sorry, it's hard to tell what action and results you are referring to: "put it at the top" and "broke my django" aren't very precise. Can you elaborate? – Ned Deily Oct 12 '14 at 06:40
  • For more about how sys.path works, see this answer http://stackoverflow.com/a/38403654/850326 – djhaskin987 Jul 15 '16 at 20:17
  • 2
    just a side note: `sys.path` is zero-indexed as any list, so it should be `sys.path.insert(0, '/path/to/mod_directory')` – HiFile.app - best file manager Mar 06 '17 at 09:00
  • @V.K.Thanks for noticing that after all this time! Fixed. – Ned Deily Mar 07 '17 at 15:00
  • 5
    You really should use "1" instead of "0"! Otherwise you break [sys.path](https://docs.python.org/3/library/sys.html#sys.path). See also http://stackoverflow.com/q/10095037/125507. – kadee Feb 14 '18 at 09:01
18

You could use:

import os
path = 'the path you want'
os.environ['PATH'] += ':'+path
12

As to me, i need to caffe to my python path. I can add it's path to the file /home/xy/.bashrc by add

export PYTHONPATH=/home/xy/caffe-master/python:$PYTHONPATH.

to my /home/xy/.bashrc file.

But when I use pycharm, the path is still not in.

So I can add path to PYTHONPATH variable, by run -> edit Configuration.

enter image description here

Jayhello
  • 5,931
  • 3
  • 49
  • 56
  • The simplest is the best answer, add this for development will make the autocomplete work – Gang Aug 10 '18 at 22:25
  • I have updated the .bashrc as explained above but did not reflect in the jupyter notebook. Jupyter notebook still says module not found. – Athar Jan 09 '19 at 14:03
2

When running a Python script from Powershell under Windows, this should work:

$pathToSourceRoot = "C:/Users/Steve/YourCode"
$env:PYTHONPATH = "$($pathToSourceRoot);$($pathToSourceRoot)/subdirs_if_required"

# Now run the actual script
python your_script.py
Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
1

Temporarily changing dirs works well for importing:

cwd = os.getcwd()
os.chdir(<module_path>)
import <module>
os.chdir(cwd)
  • This doesn't seem to work; fails here on Python 3.8.11 with `No module named ''`. – ssc Aug 30 '21 at 04:34