5

I wanted to ask if there is a way to import modules/functions from a folder in a project that is adjacent to another folder.

For example lets say I have two files:

project/src/training.py
project/lib/functions.py

Now both these folders have the __init__.py file in them. If I wanted to import functions.py into training.py, it doesn't seem to detect. I'm trying to use from lib.functions import * .I know this works from the upper level of the folder structure, where I can call both files from a script, but is there a way to do it files in above/sideways folders?

mlenthusiast
  • 1,094
  • 1
  • 12
  • 34
  • This question is inconsistent. These are files, not folders. The init file is `__init__.py`, not `__init.py__`. You'd import one module from the other with `import functions`. Please create a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) – zvone Apr 02 '19 at 22:15
  • If the enclosing folder `project` has a `__init__.py` file in it, you can use relative imports: `from ..lib import functions`, or `from ..lib.functions import *` or something – Green Cloak Guy Apr 02 '19 at 22:19
  • Sorry, had some typos. Does this make more sense now? – mlenthusiast Apr 02 '19 at 22:19
  • @GreenCloakGuy doesn't seem to work – mlenthusiast Apr 02 '19 at 22:27
  • There's a wealth of questions about this topic: https://stackoverflow.com/q/4383571/1394393, https://stackoverflow.com/q/16981921/1394393, https://stackoverflow.com/q/2349991/1394393, https://stackoverflow.com/q/279237/1394393, https://stackoverflow.com/q/72852/1394393 Admittedly, though, most of the top solutions are all very bad advice. – jpmc26 Apr 03 '19 at 03:57

3 Answers3

8

Fundamentally, the best way of doing this depends on exactly how the two modules are related. There's two possibilities:

  1. The modules are part of one cohesive unit that is intended to be used as a single whole, rather than as separate pieces. (This is the most common situation, and it should be your default if you're not sure.)
  2. The functions.py module is a dependency for training.py but is intended to be used separately.

Cohesive unit

If the modules are one cohesive unit, this is not the standard way of structuring a project in Python.

If you need multiple modules in the same project, the standard way of structuring the folders is to include all the modules in a single package, like so:

project/
    trainingproject/
        __init__.py
        training.py
        functions.py
    other/
        ...
    project/
        ...
    folders/
        ...

The __init__.py file causes Python to recognize the trainproject/ directory as a single unit called a package. Using a package enables to use of relative imports:

training.py

from . import functions

# The rest of training.py code

Assuming your current directory is project, you can then invoke training.py as a module:

python -m trainingproject.training

Separate units

If your modules are actually separate packages, then the simplest idiomatic solutions during development is to modify the PYTHONPATH environment variable:

sh-derviative syntax:

# All the extra PYTHONPATH references on the right are to append if it already has values
# Using $PWD ensures that the path in the environment variable is absolute.
PYTHONPATH=$PYTHONPATH${PYTHONPATH:+:}$PWD/lib/
python ./src/training.py

PowerShell syntax:

$env:PYTHONPATH =  $(if($env:PYTHONPATH) {$env:PYTHONPATH + ';'}) + (Resolve-Path ./lib)
python ./src/training.py

(This is possible in Command Prompt, too, but I'm omitting that since PowerShell is preferred.)

In your module, you would just do a normal import statement:

training.py

import functions

# Rest of training.py code

Doing this will work when you deploy your code to production as well if you copy all the files over and set up the correct paths, but you might want to consider putting functions.py in a wheel and then installing it with pip. That will eliminate the need to set up PYTHONPATH by installing functions.py to site-packages, which will make the import statement just work out of the box. That will also make it easier to distribute functions.py for use with other scripts independent of training.py. I'm not going to cover how to create a wheel here since that is beyond the scope of this question, but here's an introduction.

jpmc26
  • 28,463
  • 14
  • 94
  • 146
  • Thanks! this is what I was looking for, thanks for the comprehensive answer. I guess I do have to go the python path way since they are separate modules. I knew how to package the cohesive unit way, but this definitely covers all aspects of it – mlenthusiast Apr 03 '19 at 16:50
  • For the separate units creating a setup.py and running pip install -e has worked well. – Brent Oct 27 '21 at 20:26
1

Yes, it’s as simple as writing the entire path from the working directory:

from project.src.training import *

Or

from project.lib.functions import *
koopmac
  • 936
  • 10
  • 27
0

I agree with what polymath stated above. If you were also wondering how to run these specific scripts or functions once they are imported, use: your_function_name(parameters), and to run a script that you have imported from the same directory, etc, use: exec(‘script_name.py). I would recommend making functions instead of using the exec command however, because it can be a bit hard to use correctly.

LiamººT
  • 69
  • 2