Josja
Josja

Reputation: 141

Python import: resolve conflict between current directory and external library

The 101'th times things like these are asked, but still I didn't find any solution for this:

I have 3 python files:

main.py uses functions from math.py and site.py
site.py uses functions from math.py
math.py works independently

Each of these files can contain a main function that's executed if __ name __ == __ main __ and thus the command "python3 file.py" should be able to handle all imports for 'file' = main/site/math.

A second issue is that the filenames site and math are also names of standard libraries.

Besides, the program should be portable in the sense that imports should not contain absolute paths to the other python files.

What are the import statements needed?

Try 1)

All files are in the same directory

main.py

import site, math

site.py

import math

Problem: standard libraries are imported instead.

Try 2)

site.py and mathtools.py (renaming of math.py for this example) are in a subdirectory 'include' of the directory containing main.py

main.py

import include.site, include.mathtools

site.py

import mathtools

Problem: while site.py can be runned without problems, the statement 'import mathtools' leads to an unknown module when main.py is runned.

Try 3)

site.py and math.py are in a subdirectory 'include' of the directory containing main.py

main.py

import sys, os 
sys.path.append(os.path.abspath("./include"))
import include.site, include.math

site.py

import math

Problem: while the issue in try 2 is resolved, there's still a confict since math is confused with the standard library.

My ideal solution: Is there a simple way to state "import from a file in the same directory as the python file which contains this import statement, not from the standard library"

Thanks you on beforehand!

Upvotes: 3

Views: 3180

Answers (1)

Column01
Column01

Reputation: 151

The easiest solution here is to not shadow built in modules with custom ones, and use absolute paths but regardless this might help you out with some more information:

Python will execute an imported module's function from the directory it is called from.

For example, a function called output_file is in the script test1.py in the directory tests/test1.py and will output a text file called test1.txt when run. If you run that function from the script test1.py directly, it will output the text file to /tests/test1.txt. If you were to then import test1.py into a script in the root directory and call the function, it will output the file to the root directory instead.

**Directory path for visual**
├── main.py
├── (if imported and run from main, "test1.txt" is here)
├── tests
│   ├── test1.py
│   ├── (if run from test1 directly, "test1.txt" is here)

All the import statements are executed like you would be importing from the main script (meaning for attempt 3 above, it will import the builtin math.py cause site.py gets run as if it was from the root directory, and doesn't have the absolute path to include/math.py.)

Upvotes: 1

Related Questions