49

I keep seeing sites mentioning that the directory that you execute 'python ' get added to the python path. For example on http://www.stereoplex.com/blog/understanding-imports-and-pythonpath, the author cd's to the /tmp folder then does 'print(sys.path)' and lo and behold, the /tmp folder appears in the path list. Here is me trying this out on my system (with 2.6.6 installed):

example structure:

app/
  mymodule.py
  inner_folder/
    myscript.py

in myscript.py contains the line:

import mymodule.py

what I did:

cd app
python inner_folder/myscript.py # ImportError

Since I am executing the interpreter from the app/ directory, shouldn't 'app' be added to the python path? This is how a lot of the docs I have been reading have specified the behaviour should be.

Please enlighten!

(I have temporarily solved this by manually adding the folder I want into the environment but don't want to rely on that forever. Since many sites say this can be done, I'd like to reproduce it for myself)

Aman
  • 11
  • 3
trinth
  • 5,919
  • 9
  • 40
  • 45

4 Answers4

78

It is the script's directory that is added, not the current directory. If you turn inner_folder/ into a package then you can use python -m inner_folder.myscript in order to run the script while having app/ added to sys.path.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • +1: indeed, even with the import fixed, this is the real solution. – André Caron Jun 20 '11 at 19:40
  • 4
    Thank you for clarifying. I knew the script's directory was added but the only reason I believed that the "current directory" was added as well was because it used to work. I swear! Something must have happened to make it stop working. I don't recall modifying the PYTHONPATH in any way so this is why I asked this question. – trinth Jun 20 '11 at 22:45
  • 1
    When running Python 2.7 in mac OS 10.7.5, the current directory is added for me, and the original example code works. – Cypress Frankenfeld Jun 19 '13 at 18:46
  • @CypressFrankenfeld, not sure why your current directory is added, but just for a sanity check, you might want to first execute `unset PYTHONPATH`. – Garrett Jun 20 '15 at 05:18
  • 8
    @CypressFrankenfeld: in python versions prior to 3.4, an empty PYTHONPATH resulted in '.' being added to sys.path. See https://docs.python.org/3/whatsnew/3.4.html#changes-in-python-command-behavior – ForeverWintr Sep 09 '16 at 21:41
4

Whether or not the current directory is in sys.path, import statements usually look like:

import mymodule

The code you wrote looks like:

import 'mymodule.py'
André Caron
  • 44,541
  • 12
  • 67
  • 125
0

Check that the module directory is not empty. That may sound stupid, but in my case I hadn't realised that it was a git submodule and hadn't cloned recursively.

Anthony Hayward
  • 2,164
  • 21
  • 17
0

In my experience, the cleanest solution is to add a setup.py like this structure:

app/
  __init__.py
  mymodule.py
  inner_folder/
    myscript.py
setup.py

And the content of setup.py looks like this:

from setuptools import setup

setup(
    name='my-app',
    version='0.1',
    packages=['app'],
    install_requires=[
          # a list of required packages if needed
      ],
)

After installing via python setup.py develop, in myscript.py, you can import mymodule like this:

import app.mymodule

Or if you want to do import mymodule, you can move setup.py inside app/, same directory as mymodule.py, and change packages=['app'] to packages=[],.

Tong Zhou
  • 566
  • 5
  • 7