[英]Import module inside another module in Python
I'm having a hard time trying to separate some utility code as a module.我很难尝试将一些实用程序代码分离为一个模块。 I have the the following file structure:
我有以下文件结构:
scripts/
|
+-- toggle_keyboard.py
PyUtils/
|
+-- sistema.py
+-- strings.py
+-- test_sistema.py
Sistema.py needs a class in strings.py, so it begins with: Sistema.py 在 strings.py 中需要一个 class,所以它以:
from strings import String
And the Pytest test_sistema.py needs sistema.py, so it starts with:而 Pytest test_sistema.py 需要 sistema.py,所以它以:
import sistema as sis
Now, this is all fine when I run the tests with PyTest. However, I can't use this module in toggle_keyboard.py.现在,当我使用 PyTest 运行测试时一切正常。但是,我不能在 toggle_keyboard.py 中使用这个模块。 I start it with:
我开始它:
import PyUtils.sistema as sis
And that gets me no compilation errors.这让我没有编译错误。 However, when I run it, this is what I get:
但是,当我运行它时,这就是我得到的:
Traceback (most recent call last):
File "toggle_keyboard.py", line 2, in <module>
import PyUtils.sistema as sis
File "/home/xxxxxx/scripts/PyUtils/sistema.py", line 2, in <module>
from strings import String
ModuleNotFoundError: No module named 'strings'
Searching for similar problems online, I found that making use of "relative imports" could solve the problem.在网上搜索了类似的问题,发现利用“相对导入”可以解决问题。 Indeed, I was able to run toggle_keyboard.py when I changed sistema.py with this:
事实上,当我用这个更改 sistema.py 时,我能够运行 toggle_keyboard.py:
from .strings import String
However, the test doesn't run anymore.但是,测试不再运行。 When I execute PyTest now, I get:
当我现在执行 PyTest 时,我得到:
Traceback:
/usr/lib/python3.6/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
PyUtils/test_sistema.py:1: in <module>
import sistema as sis
PyUtils/sistema.py:2: in <module>
from .strings import String
E ImportError: attempted relative import with no known parent package
What would be a solution to make both the main script and the module's test to work?使主脚本和模块的测试都起作用的解决方案是什么?
In your sistema.py
file try:在你的
sistema.py
文件中尝试:
from PyUtils.strings import String
and in test_sistema.py
在
test_sistema.py
中
import PyUtils.sistema as sis
and ensure your PyUtils
directory contains an __init__.py
file and that should fix the issue.并确保您的
PyUtils
目录包含一个__init__.py
文件,这应该可以解决问题。
An alternative approach although one that is typically not recommended: Add the top 3 lines from below in any file in the scripts
dir that imports from the PyUtils
dir.另一种方法虽然通常不推荐:从
PyUtils
目录导入的scripts
目录中的任何文件中,从下面添加前 3 行。
toggle_keyboard.py
import sys
from pathlib import Path
sys.path.insert(0, Path(__file__).parent / "PyUtils")
from PyUtils.systema as sis
from PyUtils.strings import String
...
then the files in PyUtils
can go back to:那么
PyUtils
中的文件可以 go 回到:
from strings import String
import sistema as sis
You would still want to have the __init__.py
in PyUtils
dir.您仍然希望在
PyUtils
目录中拥有__init__.py
。
And another approach would be to catch exceptions on the imports and try alternatives like so....另一种方法是捕获导入的异常并尝试像这样的替代方案......
sistema.py
try:
from strings import String
except ImportError:
from PyUtils.strings import String
test_sistema.py
try:
import sistema.py as sis
except ImportError:
import PyUtils.sistema as sis
This is actually the better option in your situation.在您的情况下,这实际上是更好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.