python – 导入另一个文件错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 导入另一个文件错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2413字,纯文字阅读大概需要4分钟。
内容图文
![python – 导入另一个文件错误](/upload/InfoBanner/zyjiaocheng/781/1c592390cfca4c91b5c2b7be0f002521.jpg)
我的文件夹/文件structrue是:
testpkg/test/__init__.py;
testpkg/test/test1.py
testpkg/test/test2.py
testpkg/setup.py
testpkg / test / __ init__.py文件为空.
testpkg / test / test1.py文件内容:
class Test1:
def __init__(self, name):
self.name = name
def what_is_your_name(self):
print(f'My name is {self.name}')
testpkg / test / test2.py文件内容:
from .test1 import Test1
def main():
t = Test1('me')
t.what_is_your_name()
if __name__ == '__main__':
main()
/testpkg/setup.py内容:
from setuptools import setup
setup(name='test',
version='0.1',
packages=['test'],
entry_points={
'console_scripts': [
'test_exec = test.test2:main'
]
}
)
我无法直接调试/运行test2.py脚本,因为它给了我错误:
? python test/test2.py
Traceback (most recent call last):
File "test/test2.py", line 1, in <module>
from .test1 import Test1
ModuleNotFoundError: No module named '__main__.test1'; '__main__' is not a package
但是当我用pip install -U安装它时.
有用:
? pip install -U .
Processing /home/kossak/Kossak/files_common/PythonProjects/testpkg
Installing collected packages: test
Found existing installation: test 0.1
Uninstalling test-0.1:
Successfully uninstalled test-0.1
Running setup.py install for test ... done
Successfully installed test-0.1
? test_exec
My name is me
问题是:如何正确编写test2.py以便它可以在两种方式下工作 – 直接(因此我可以在PyCharm中调试它或者只运行python test2.py)并在安装测试包之后?我试过换线:
from .test1 import Test1
至
from test1 import Test1
(删除点)
我可以从命令行运行test2.py,但是在安装之后,我的脚本“test_exec”给了我错误:
Traceback (most recent call last):
File "/home/kossak/anaconda3/bin/test_exec", line 11, in <module>
load_entry_point('test==0.1', 'console_scripts', 'test_exec')()
File "/home/kossak/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 565, in load_entry_point
File "/home/kossak/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2598, in load_entry_point
File "/home/kossak/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2258, in load
File "/home/kossak/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2264, in resolve
File "/home/kossak/anaconda3/lib/python3.6/site-packages/test/test2.py", line 1, in <module>
from test1 import Test1
ModuleNotFoundError: No module named 'test1'
解决方法:
尝试像这样导入它:从test.test1导入Test1
内容总结
以上是互联网集市为您收集整理的python – 导入另一个文件错误全部内容,希望文章能够帮你解决python – 导入另一个文件错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。