繁体   English   中英

从 `console_scripts` 运行脚本但通过 Python 控制台导入时不会出现 `ModuleNotFoundError`

[英]`ModuleNotFoundError` when running script from `console_scripts` but not when importing via Python console

我正在尝试设置适当的 Python package 进行分发,但我的setup.py文件的console_scripts属性遇到问题。 我的设置中有以下内容:

<...>
entry_points={"console_scripts": ["solve_cipher=cipher_solver.solve_cipher:main"]}
<...>

我已经打包并发送到测试 PyPI,然后将我的打包安装在一个新的虚拟环境中。 现在,命令solve_cipher可用,但运行它会给我:

$ solve_cipher 
Traceback (most recent call last):
  File "/Users/markus/.virtualenvs/cipher_solver_debug_pypi/bin/solve_cipher", line 6, in <module>
    from cipher_solver.solve_cipher import main
ModuleNotFoundError: No module named 'cipher_solver'

但是,如果我只是启动 Python 并运行相同的导入行,它就可以正常工作:

$ python
>>> from cipher_solver.solve_cipher import main
>>>

为什么会这样? 我的项目布局如下:

cipher_solver
    <...>
    cipher_solver
        consts.py
        simple.py
        solve_cipher.py
        utils.py
    <...>
    setup.py

我发现了这个问题。 我在setup.py中使用setuptools.find_packages() ,结果发现该方法需要包和子包中的__init__.py文件才能找到它们。 我认为这在 Python 3.3+ 中不再需要,但显然在某些情况下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM