ModulenotFoundError试图运行使用setuptools创建可执行文件时

发布于 2025-02-02 18:00:53 字数 1226 浏览 1 评论 0原文

这是我的项目结构

/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2025-02-09 18:00:53

我通过将run.py中的导入语句更改为:

from app import hello_world

setup.py到:

scripts=['foo/app.py', 'foo/run.py'],
entry_points={
    'console_scripts': [
        'hello-world=run:main'
    ]
}

使用pip安装后,app.pyrun.py正在添加到: /bin似乎可以解决这个问题。

I solved it by changing the import statement in run.py to:

from app import hello_world

And setup.py to:

scripts=['foo/app.py', 'foo/run.py'],
entry_points={
    'console_scripts': [
        'hello-world=run:main'
    ]
}

After installing using pip, app.py and run.py are being added to /bin which seems to solve the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文