ModulenotFoundError试图运行使用setuptools创建可执行文件时
这是我的项目结构
/Users/tom/PycharmProjects/foo
├── __init__.py
├── foo
│ ├── __init__.py
│ ├── app.py
│ └── run.py
└── setup.py
app.py:run.py:setup.py
def hello_world():
print("Hello world")
运行可执行的foo时 /bin /bin时,
from foo.app import hello_world
def main():
hello_world()
:
from setuptools import setup, find_packages
setup(
name='foo',
version='0.0.1',
packages=find_packages(),
entry_points={
'console_scripts': [
'foo=foo.run:main',
]
}
)
使用pip安装后,我有以下文件夹结构:
/Users/tom/Desktop/foo/
├── bin
│ └── foo
├── foo
│ ├── __init__.py
│ ├── app.py
│ └── run.py
└── foo-0.0.1.dist-info
├── INSTALLER
├── METADATA
├── RECORD
├── REQUESTED
├── WHEEL
├── direct_url.json
├── entry_points.txt
└── top_level.txt
我会收到以下错误:
Traceback (most recent call last):
File "/Users/tom/Desktop/foo/bin/foo", line 5, in <module>
from foo.run import main
ModuleNotFoundError: No module named 'foo'
如何使可执行执行工作没有必须激活虚拟环境并从中运行它?
This is my project structure
/Users/tom/PycharmProjects/foo
├── __init__.py
├── foo
│ ├── __init__.py
│ ├── app.py
│ └── run.py
└── setup.py
app.py:
def hello_world():
print("Hello world")
run.py:
from foo.app import hello_world
def main():
hello_world()
setup.py:
from setuptools import setup, find_packages
setup(
name='foo',
version='0.0.1',
packages=find_packages(),
entry_points={
'console_scripts': [
'foo=foo.run:main',
]
}
)
After using pip to install I have the following folder structure:
/Users/tom/Desktop/foo/
├── bin
│ └── foo
├── foo
│ ├── __init__.py
│ ├── app.py
│ └── run.py
└── foo-0.0.1.dist-info
├── INSTALLER
├── METADATA
├── RECORD
├── REQUESTED
├── WHEEL
├── direct_url.json
├── entry_points.txt
└── top_level.txt
When running the executable foo in /bin I am getting the following error:
Traceback (most recent call last):
File "/Users/tom/Desktop/foo/bin/foo", line 5, in <module>
from foo.run import main
ModuleNotFoundError: No module named 'foo'
How can I make the executable work without having to activate a virtual environment and run it from it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过将run.py中的导入语句更改为:
setup.py到:
使用pip安装后,
app.py
和run.py
正在添加到: /bin似乎可以解决这个问题。I solved it by changing the import statement in run.py to:
And setup.py to:
After installing using pip,
app.py
andrun.py
are being added to /bin which seems to solve the issue.