- Import sub1 normally from main.py
Currently, I am writing a python3 program with the following directory structure and source code.
├── main.py └── modules ├── sub1.py └── sub2.py
# main.py from modules import sub1 if __name__ == '__main__': sub1.sub1_func ()
# modules/sub1.py import sub2 def sub1_func (): sub2.sub2_func () if __name__ == '__main__': sub1_func ()
# modules/sub2.py def sub2_func (): print ('sub2')
main.py imports sub1.py and uses sub1_func () in sub1,
sub1_func () refers to sub2.py sub2_func () in the same directory as sub1.py.
If i run main.py at this time,
ModuleNotFoundError: No module named 'sub2'will appear.
Therefore, considering the thing seen from main.py, the sub1.py import statement was changed from
from modules import sub2The correct operation as expected was confirmed. ("Sub2" is output)
However, when sub1.py is executed in this state,
ModuleNotFoundError: No module named 'modules'will appear.
sub1 is going to be referenced from other modules, so is there any way to import sub1 normally from main.py with import of sub1 as
import sub2? .
In the first place, isn't it very good to have such a file structure?
If i am familiar with python, would you please teach me?
Answer # 1
Rewrite import of
# modules/sub1.py from .import sub2
Answer # 2
The python import is referenced relative to the path when it is executed.
To reference relative to the location of the file instead of the runtime path
Like, you need to write explicitly.
- python - i want to store multiple json files in sqlite at once using glob
- python - i want to perform the same process for multiple xlsx files in a folder and save them with "_aggregate" at the
- python - multiple exports from reading csv files
- python - i want to import from config
- python - i want to link the search results of external urls to multiple randomly generated keywords in django
- can multiple file io by built-in function open using multithreading in python be expected to be faster?
- [python: django] i want to create multiple gallery pages linked to a model
- python 3x - a i want to get the value of values of the line selected in the list of files
- python - django i want to get multiple child tables in a for loop of a parent table
- python - i don't know how to read and write files in binary (bitwise)
- python - about multiple processing and loop processing in discordpy
- cannot combine python pdf files (multiple processing using excel list and for statement)
- python (pandas): how to create multiple dataframes using for statement and output them to tsv file
- i want to calculate multiple centroids using python and opencv
- python - i have a question about multiple updates of pymongo
- python - an error occurred in the code of "from smbus2 import smbus" on raspberry pi 4 (smbus2 installed)
- python 3x - i want to combine a large number of csv files in the line direction
- python 3x - text files are garbled in python
- python delete files after uploading
- problem that multiple files are created when using logger in java
- python - the library installed by pip cannot be imported no module named'selenium'
- python: can the function called in main () refer to the ftp module? nameerror
- python - i get an error when connecting to a voice channel with discordpy
- i want to extract one character string of text that exists in multiple files with python
- [python 390] seven-series if statement does not work
- python - wait for user reaction using wait_for method in discordpy
- about combining when there are duplicate values in python dictionary type
- python - i don't know how to process blocking processes in parallel
- i want to connect to 64-bit ms access from 32-bit python
- i want to wait until the next process can be performed after executing the process in python