3

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!

asked May 13, 2019 at 14:27

1 Answer 1

1

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.)

answered May 13, 2019 at 17:04
Sign up to request clarification or add additional context in comments.

4 Comments

@Josja did this answer your question? Please mark it if it did.
This doesn't help. It doesn't say what the imports should be for the given problem.
I explained that it's because he is overwriting the builtin module name. Don't call it math.py if you need to import a custom module directly, that he needs to do some using an absolute path to the module... what did I not answer in the question? Do you also have this problem? @HeatherSawatsky
I did have the same issue, yes. I fixed it using a relative import with a leading period: from .types import MyType. But it also turned out, at the time of making my previous comment, I had a fundamental misunderstanding of packages/modules and was running my code from the wrong location in the first place (I'm new to python). With my new understanding and the new relative import, my problem's all fixed.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.