[英]File in current folder not found when installed with pip
Python module with an entry point (same as here ).带有入口点的 Python 模块(与此处相同)。 The
__main__
file imports another: __main__
文件导入另一个:
from myfile import MyClass
myfile.py
is a file in the same folder, mymodule
. myfile.py
是同一文件夹中的文件mymodule
。 It contains the class called MyClass
.它包含名为
MyClass
的类。 This works under regular Python, if I run python3 __main__.py
.这适用于常规 Python,如果我运行
python3 __main__.py
。
Now, I've installed mymodule
as a module with sudo pip3 install .
现在,我已经使用
sudo pip3 install .
将mymodule
安装为一个模块sudo pip3 install .
on MacOS X (EDIT: same error on Linux).在 MacOS X 上(编辑:Linux 上同样的错误)。 When I run
mymodule
from the command line, I get a ModuleNotFoundError
for myfile:当我从命令行运行
mymodule
,我收到了 myfile 的ModuleNotFoundError
:
Traceback (most recent call last):
File "/Users/valekseyev/.pyenv/versions/3.7.3/bin/mymodule", line 11, in <module>
load_entry_point('mymodule==0.50', 'gui_scripts', 'mymodule')()
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2793, in load_entry_point
return ep.load()
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2411, in load
return self.resolve()
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2417, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/mymodule/__main__.py", line 5, in <module>
from myfile import MyClass
ModuleNotFoundError: No module named 'myfile'
The file myfile.py
is present alongside __main__.py
under /Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/mymodule/
, I've checked.文件
myfile.py
与__main__.py
一起出现在/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/mymodule/
下,我已经检查过。 There's an __init__.py
in the folder, too, it's blank.文件夹中也有一个
__init__.py
,它是空白的。 The folder is listed under packages
in setup.py
.该文件夹列在
setup.py
packages
下。
What's going on please?请问这是怎么回事?
EDIT: looking at Python modules out there, they import other files from the same folder with the following syntax:编辑:查看 Python 模块,它们使用以下语法从同一文件夹导入其他文件:
from .myfile import MyClass
If I try that, the regular Python runs break:如果我尝试这样做,则常规 Python 运行会中断:
ImportError: attempted relative import with no known parent package
导入错误:尝试在没有已知父包的情况下进行相对导入
Any way around that, maybe?有什么办法,也许?
Found one way around.找到了一种方法。 I've prepended the file names with .
我在文件名前加上 . in the import lines, created a file one level up (on the same level as
setup.py
) that would treat the mymodule
folder as a package:在导入行中,创建了一个上一级的文件(与
setup.py
处于同一级别),它将mymodule
文件夹视为一个包:
from mymodule.__main__ import main
main()
and I can run that.我可以运行它。 There's yet another issue I'm facing (library modules take precedence over patched versions), but not this one :)
我还面临另一个问题(库模块优先于修补版本),但不是这个:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.