Writing from the Top Directory in a Python Import Statement

Asked 2 years ago, Updated 2 years ago, 19 views

I would like to ask you about how to write import statements including the top tier in the project rules and regulations.
The following is an example:

/root_package*I want to import from here.
 | | start.py
 | Definition of const.py*XYZ=(1,0,0)
 | 
 /utils
  | | u.py
  | uconst.py *Definitions such as ABC=(1,2,3)

start.py

from root_package.const import XYZ

u.py

 from root_package.utils.uconst import ABC

The example is omitted, but "_init__.py" is empty in each folder.
As mentioned above,
with the top-level "root_package" on start.py and u.py I think there is no problem with the from import description.
"ModuleNotFoundError XYZ" appears when running start.py.
Isn't there enough description?

python

2022-09-30 13:45

1 Answers

It seems that the program you are running is placed in the wrong location.

If start.py is the program that runs, it is an application, not a file in the package, so it is strange that it is in the root_package directory.

start.py should be placed in the parent directory of root_package as follows:

start.py
/root_package *I want to import from here
  | __init__.py
  | Definition of const.py*XYZ=(1,0,0)
  | 
  /utils
    | __init__.py
    | | u.py
    | uconst.py *Definitions such as ABC=(1,2,3)


2022-09-30 13:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.