Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a Python script from a subdirectory without breaking upper-level imports?

I have a very simple scenario like this:

example/
    common.py
    folder1/
        script.py

where script.py file should import common.py module.

If I cd into folder1/ and run the script (i.e. by calling python3 script.py on the command line), the import breaks with the usual error ModuleNotFoundError: No module named 'common'.

I know I could turn the whole parent folder into a package by adding __init__.py files in each subdirectory, however this solution still prevents me to run the script directly from inside folder1/.

How can I fix this?

like image 889
Ignorant Avatar asked Aug 29 '19 15:08

Ignorant


2 Answers

If you turn both directories into python packages, and the top directory is in your PYTHONPATH, you can run script.py like so:

python3 -m example.folder1.script
like image 93
WavesAtParticles Avatar answered Sep 23 '22 00:09

WavesAtParticles


If you need to add a parent directory to the search path, you can always do:

import os
import sys

DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.insert(0, DIR)

os.path.realpath(__file__) gets the realpath to the file, and os.path.dirname gets the directory name of this file. Doing it twice gets the example directory here. Now, since this is added to the search path, you can import common.

However, you should really consider having a dedicated scripts or bin directory, rather than try to run scripts from subdirectories. Think about writing a library, and import this library rather than individual files.

like image 22
Alexander Huszagh Avatar answered Sep 20 '22 00:09

Alexander Huszagh