84
votes

I have the following directory structure:

application
    tests
        main.py
    main.py

application/main.py contains some functions.

tests/main.py will contain my tests for these functions but I can't import the top level main.py. I get the following error:

ImportError: Import by filename is not supported.

I am attempting to import using the following syntax:

import main

What am I doing wrong?

7
I'd read the documentation first: docs.python.org/2/tutorial/modules.html#packages - Blender
Well you can't import the same file name you are working on. - squiguy
In which file are you doing import main ? - Aivar

7 Answers

79
votes

If you'd like your script to be more portable, consider finding the parent directory automatically:

import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# import ../db.py
import db
34
votes

You must add the application dir to your path:

import sys
sys.path.append("/path/to/dir")
from app import object

Or from shell:

setenv PATH $PATH:"path/to/dir"

In case you use windows: Adding variable to path in windows.

Or from the command line:

set PATH=%PATH%;C:\path\to\dir
21
votes

First of all you need to make your directories into packages, by adding __init__.py files:

application
    tests
        __init__.py
        main.py
    __init__.py
    main.py

Then you should make sure that the directory above application is on sys.path. There are many ways to do that, like making the application infto a package and installing it, or just executing things in the right folder etc.

Then your imports will work.

6
votes

You cannot import things from parent/sibling directories as such. You can only import things from directories on the system path, or the current directory, or subdirectories within a package. Since you have no __init__.py files, your files do not form a package, and you can only import them by placing them on the system path.

3
votes

To import a file in a different subdirectory of the parent directory, try something like this:

sys.path.append(os.path.abspath('../other_sub_dir'))
import filename_without_py_extension

Edit: Missing closing bracket.

0
votes

Late to the party - all other answers are wrong here unfortunately - apart @LennartRegebro's (and @BrenBarn's) which is incomplete. For the benefit of future readers - the OP should, first of all, add the init files as in

root
    application
        tests
            __init__.py
            main.py
        __init__.py
        main.py

then:

$ cd root
$ python -m application.tests.main

or

$ cd application
$ python -m tests.main

Running a script directly from inside its package is an antipattern - the correct way is running with the -m switch from the parent directory of the root package - this way all packages are detected and relative/absolute imports work right.

-2
votes

in python . exists for same directory, .. for parent directory to import a file from parent directory you can use ..

from .. import filename (without .py extension)