[英]How do I import and use a python module that imports classes from another file within itself?
I am an experienced C# developer but have recently had to write some python code. 我是一位经验丰富的C#开发人员,但最近不得不编写一些python代码。 I really like the language but I'm struggling with splitting some of my code into modules. 我非常喜欢这种语言,但我正在努力将我的一些代码分成模块。 For simplicity, I have essentially 2 modules with the first looking like this: 为简单起见,我基本上有2个模块,第一个看起来像这样:
file1.py file1.py
from file2 import addOperation, volumeOperation
class utils:
def add(self, num1, num2):
op = addOperation(num1, num2)
return op.calculate()
def volume(self, length, width, height):
op = volumeOperation(length, width, height)
return op.calculate()
def main():
util = utils()
print(f"Adding 2 and 3 produces { util.add(2, 3) }")
print(f"Volume of a 2x2x2 cube is { util.volume(2,2,2) }")
if __name__ == '__main__':
main()
file2.py file2.py
class addOperation:
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def calculate(self):
return self.num1 + self.num2
class volumeOperation:
def __init__(self, length, width, height):
self.width = width
self.height = height
self.length = length
def calculate(self):
return self.width * self.height * self.length
_init__.py _init__.py
import file1, file2
Those 3 files sit in a folder called "mylibrary" and when I run "python .\\file1.py" everything works as expected. 这三个文件位于名为“mylibrary”的文件夹中,当我运行“python。\\ file1.py”时,一切都按预期工作。 The module then gets built into a wheel using this setup.py: 然后使用此setup.py将模块内置到轮子中:
import setuptools
print(setuptools.find_packages())
setuptools.setup(
name="mylibrary",
version="1.0.0",
author="me",
author_email="me@me.com",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent"
],
)
The resulting wheel then gets installed using "python -m pip install --user mylibrary-1.0.0-py3-none-any.whl" which appears to work just fine. 然后使用“python -m pip install --user mylibrary-1.0.0-py3-none-any.whl”安装得到的轮子,看起来效果很好。
My second module is the following file "consumer.py": 我的第二个模块是以下文件“consumer.py”:
import mylibrary.file1
if __name__ == '__main__':
util = mylibrary.file1.utils()
print(f"Adding 2 and 3 produces { util.add(2, 3) }")
print(f"Volume of a 2x2x2 cube is { util.volume(2,2,2) }")
When I try to run "python .\\consumer.py" (I'm on windows using python 3.7, btw) I get "ModuleNotFoundError: No module named 'file2'". 当我尝试运行“python。\\ consumer.py”(我在Windows上使用python 3.7,顺便说一句)时,我得到“ModuleNotFoundError:没有名为'file2'的模块”。
I have tried different ways of importing, I've tried using an empty __init__.py
in the mylibrary module, and I have read a few other stackoverflow questions that at least have similar problems but I haven't been able to get it to work. 我尝试了不同的导入方式,我尝试在mylibrary模块中使用空的__init__.py
,我已经阅读了一些其他stackoverflow问题,至少有类似的问题,但我无法让它工作。
What should I modify for this to work? 我应该修改什么才能使用?
In Python 3, implicit relative imports got removed : 在Python 3中, 删除了 隐式相对导入:
The only acceptable syntax for relative imports is from .[module] import name. 相对导入唯一可接受的语法来自。[module]导入名称。 All import forms not starting with . 所有导入表单都不以。 are interpreted as absolute imports. 被解释为绝对进口。 ( PEP 0328 ) ( PEP 0328 )
Your 您的
from file2 import ...
in file1.py is such an implicit relative import (ie, it would have worked as a relative import in Python 2, but is now treated as an absolute import in Python 3). 在file1.py中是一个隐式的相对导入(即,它在Python 2中作为相对导入,但现在在Python 3中被视为绝对导入)。
So, you'll need to change it to either an absolute import: 因此,您需要将其更改为绝对导入:
from mylibrary.file2 import ...
or an explicit relative import (using a leading dot): 或显式相对导入(使用前导点):
from .file2 import ...
If you really do need them, you'll also need to convert the imports in your __init__.py
to 如果你确实需要它们,你还需要将__init__.py
的导入转换为
from . import file1
from . import file2
Other than that, your setuptools distribution seems to be layed out properly, and works for me (nice minimal example BTW!). 除此之外,你的setuptools发行版似乎是正确的,并且适用于我(很简单的例子BTW!)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.