[英]Why my __init__.py doesn't work in the module?
I am trying to add 'project_root' into __init__.py
and all modules can use it, but it doesn't work.我正在尝试将 'project_root' 添加到
__init__.py
中,所有模块都可以使用它,但它不起作用。
Environment: Python 3.7.0 MACOS MOJAVE环境:Python 3.7.0 MACOS MOJAVE
file structure文件结构
·
├── __init__.py
└── a.py
the codes in __init__.py
file: __init__.py
文件中的代码:
import sys
project_root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(project_root)
and in another file在另一个文件中
print(project_root)
If I run python a.py
in the same dir,or run python a.py
out of the dir, error are the same below:如果我在同一个目录中运行
python a.py
a.py,或者在目录外运行python a.py
,错误如下:
Traceback (most recent call last):
File "a.py", line 1, in <module>
print(project_root)
NameError: name 'project_root' is not defined
My question is why it doesn't work and how to fix it.我的问题是为什么它不起作用以及如何解决它。 Another question is what if you want to share some variables for other modules in a same package, how to do it?
另一个问题是,如果你想为同一个 package 中的其他模块共享一些变量怎么办?
Let us try to understand by example.让我们试着通过例子来理解。
Suppose we have the following directory and file structure:假设我们有以下目录和文件结构:
dir_1
├── __init__.py
└── a.py
b.py
__init__.py
contains: __init__.py
包含:
import sys,os
# Just to make things clear
print("Print statement from init")
project_root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(project_root)
a.py
contains: a.py
包含:
def func():
print("func from a.py")
Suppose you start with having the following code in b.py
:假设您从
b.py
中的以下代码开始:
from dir_1.a import func
func()
Executing the above will give the following output:执行上面会给出如下output:
Print statement from init
从 init 打印语句
func from a.py来自 a.py 的函数
So, from the above, we understand that the print
statement from __init__.py
is being executed.所以,从上面我们了解到,来自
__init__.py
的print
语句正在被执行。 Now, let's add print(project_root)
in b.py
:现在,让我们在
b.py
中添加print(project_root)
:
from dir_1.a import func
func()
print(project_root)
Executing the above will result in an error saying:执行上述将导致错误提示:
...
...
NameError: name 'project_root' is not definedNameError:名称“project_root”未定义
This happened because we did not have to import the print
statement from __init__.py
it just gets executed.发生这种情况是因为我们不必从
__init__.py
导入print
语句,它只是被执行。 But that is not the case for a variable.但对于变量而言,情况并非如此。
Let's try to import the variable and see what happens:让我们尝试导入变量,看看会发生什么:
from dir_1.a import func
from dir_1 import project_root
func()
print(project_root)
Executing the above file will give the following output:执行上述文件会得到如下output:
Print statement from init
从 init 打印语句
func from a.py来自 a.py 的函数
/home/user/some/directory/name/dir_1/home/user/some/directory/name/dir_1
Long story short , you need to import the variable defined in __init__.py
or anywhere else in order to use it.长话短说,您需要导入在
__init__.py
或其他任何地方定义的变量才能使用它。
Hope this helps: )希望这可以帮助: )
You have to import the variable:您必须导入变量:
from dir import project_root
Where dir is the directory you are in dir 是你所在的目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.