Categories
importerror python python-import

Importing files from different folder

2187

I have the following folder structure:

application
├── app
│   └── folder
│       └── file.py
└── app2
    └── some_folder
        └── some_file.py

From inside in some_file.py, how do I import a function from file.py?


I tried:

from application.app.folder.file import func_name

7

  • 2

    Related: stackoverflow.com/q/43476403/674039

    – wim

    Apr 18, 2017 at 15:56

  • 2

    Reading the official documentation helped me a lot! docs.python.org/3/reference/…

    May 14, 2020 at 1:20

  • If you have a dash in the name of the subfolder, it SHOULD BE UNDERSCORE. For example my-package and inside you have my_app folder and tests folder. If my_app is named my-app, you will have import problems

    – Gonzalo

    Apr 28, 2021 at 15:01

  • Neither application nor app1, app2, folder, some_folder are packages, and do not contain __init__.py, right? If you’re going to be doing a lot of this, time to make them a package.

    – smci

    Jun 14, 2021 at 23:26

  • 6

    The fact that this is so hard and there are multiple answers about it, some of which don’t work or are hacks, is probably the worst, saddest thing about Python.

    – JohnAllen

    Jun 24 at 9:06

1136

Nothing wrong with:

from application.app.folder.file import func_name

Just make sure folder also contains an __init__.py, this allows it to be included as a package. Not sure why the other answers talk about PYTHONPATH.

28

  • 72

    Because this doesn’t cover the cases where modifying PYTHONPATH is necessary. Say you have two folders on the same level: A and B. A has an __init.py__. Try importing something from B within A.

    – msvalkon

    Mar 6, 2014 at 13:45

  • 66

    What’s inside the init.py or __init__.py file?

    – X.Creates

    May 9, 2015 at 2:16

  • 73

    @Xinyang It can be an empty file. Its very existence tells Python to treat the directory as a package.

    – jay

    May 11, 2015 at 23:24

  • 27

    This is not currently the highest voted answer, but it IS the most correct answer (for most cases). Simply create a package. It’s not hard. The other answers are needed because sometimes you might be restricted from certain system changes (creating or modifying a file, etc) like during testing.

    Mar 3, 2016 at 18:59

  • 70

    Whatever I try, this won’t work. I want to import from a “sibling” directory, so one up one down. All have __ init __.py’s, including parent. Is this python 3 -specific?

    – dasWesen

    Jun 18, 2017 at 12:54


198

When modules are in parallel locations, as in the question:

application/app2/some_folder/some_file.py
application/app2/another_folder/another_file.py

This shorthand makes one module visible to the other:

import sys
sys.path.append('../')

7

  • 38

    As a caveat: This works so long as the importing script is run from its containing directory. Otherwise the parent directory of whatever other directory the script is run from will be appended to the path and the import will fail.

    May 3, 2017 at 3:02

  • 26

    To avoid that, we can get the parent directory of file sys.path.append(os.path.dirname(os.path.abspath(__file__)))

    – Rahul

    Sep 17, 2018 at 10:09

  • 1

    That didn’t work for me – I had to add an additional dirname in there to climb back up to the parent, so that running cli/foo.py from the command line was able to import cli.bar

    – RCross

    Jul 26, 2019 at 11:23


  • 3

    @Rahul, your solution doesn’t work for interactive shells

    Nov 27, 2019 at 18:19

  • 5

    If you run it from your root folder (ie. application folder), you are probably fine with sys.path.append('.') then importing the module by using from app2.some_folder.some_file import your_function. Alternatively what works for me is running python3 -m app2.another_folder.another_file from root folder.

    – addicted

    Dec 16, 2019 at 13:23