0

I have the following code structure in python:

Application
├── app
│   ├── data/
│   ├── utils/
│   │   └── utils.py
│   └── main.py
├── utils2/
│   └── utils2.py
└── main2.py

I downloaded the code in the folder app/ from github, so when we run the file main.py, the data is imported in the utils.py file as :

from data import *

Now when I need to run main2.py, I need to access the functions in utils.py, and also the files in folder data, so for that I need to import the data in utils.py as

from app.data import *

This is creating a problem when running main.py and main2.py What is the correct way of importing and using the folders and files so that only one import statement works for both scenarios?

I am using python 3.9

1 Answer 1

1

The answer is to use absolute imports in the utils.py file as:

from app.data import *

Then to run main2.py, we need to use:

python main2.py

while being inside the app folder.

and to run main.py, we need to use:

python -m app.main

this way we can run both files without conflicts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.