Import file from parent directory?

PythonPython Import

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

Late to the party - most other answers here are not correct 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__.py files as in

root
    application
        __init__.py
        main.py
        tests
            __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 as expected.

Solution 5 - Python

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.

Solution 6 - Python

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.

Solution 7 - Python

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarty WallaceView Question on Stackoverflow
Solution 1 - Pythonuser4490090View Answer on Stackoverflow
Solution 2 - Python0x90View Answer on Stackoverflow
Solution 3 - PythonLennart RegebroView Answer on Stackoverflow
Solution 4 - PythonMr_and_Mrs_DView Answer on Stackoverflow
Solution 5 - PythonBrenBarnView Answer on Stackoverflow
Solution 6 - PythonAndy FraleyView Answer on Stackoverflow
Solution 7 - PythonStackUPView Answer on Stackoverflow