[英]Script installed with setup.py raises ImportError
I am trying to add a runnable script for my project with setup.py
.我正在尝试使用setup.py
为我的项目添加一个可运行的脚本。 I added it to the scripts=
argument of setup
.我将它添加到了setup
的scripts=
参数中。 The script works fine when I run it from the project, ./solver
.当我从项目./solver
运行它时,脚本工作正常。 I install it with sudo python setup.py install
, and try to run it with solver
, but I get ImportError: No module named 'model'
.我用sudo python setup.py install
安装它,并尝试用solver
运行它,但我得到ImportError: No module named 'model'
。 How do I correctly install and run my script with setuptools?如何使用 setuptools 正确安装和运行我的脚本?
SOLVER/
solver/
model/
__init__.py
view/
__init__.py
controller/
__init__.py
__init__.py
main.py
solver <-- starts the app
setup.py
README.md
LICENCE
setup.py
: setup.py
:
#!/usr/bin/env python3
import os
from setuptools import setup, find_packages
setup(
name='SOLVER',
version='1.0.0',
description='SOLVER app test',
author=['me'],
license='BSD',
classifiers=['Programming Language :: Python :: 3 :: Only'],
packages=['solver'],
#packages=find_packages(exclude=["doc", "tests"]),
install_requires=['numpy>=1.10.4'],
scripts=['solver/solver'],
)
solver
: solver
:
#!/usr/bin/env python3
from solver import main
main.gui_mode()
You need to list all the packages, including the sub-packages, in the packages
argument.您需要在packages
参数中列出所有包,包括子packages
。 You can use find_packages
to generate that list for you.您可以使用find_packages
为您生成该列表。 Currently, you're just installing the Python files in the solver/
directory.目前,您只是在solver/
目录中安装 Python 文件。
from setuptools import setup, find_packages
setup(
...
packages=find_packages(),
...
)
You should also use entry_points
rather than scripts
, especially when all your script does is import and call one function.您还应该使用entry_points
而不是scripts
,尤其是当您的脚本所做的只是导入和调用一个函数时。 Setuptools will build scripts from the entry points that use the correct Python binary for the env they were installed in. Setuptools 将从入口点构建脚本,这些入口点为安装它们的环境使用正确的 Python 二进制文件。
setup(
...
packages=find_packages(),
entry_points={
'console_scripts': [
'solver=solver.main:gui_mode'
]
...
}
You can install your package in development mode to get your script, rather than writing it yourself.你可以在开发模式下安装你的包来获取你的脚本,而不是自己编写。
pip install -e .
You should use pip
to install to the system as well.您也应该使用pip
安装到系统中。 It keeps track of what was installed so you can uninstall it later.它会跟踪已安装的内容,以便您以后可以卸载它。
pip install .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.