问题描述
我正在为我的项目构建一个 wheel 包,并使用 setuptools
,我的项目树 结构如下:
注意:我正在使用基于 pyenv local 的名为 demo2 的虚拟环境和 python 3.7.5
$tree -L 3
.
├── Makefile
├── README.md
├── Feedback_report.csv
├── pyproject.toml
├── setup.py
├── src
│ ├── __init__.py
│ └── mypkg
│ ├── QuestionGeneration
│ ├── README.md
│ ├── api
│ ├── data
│ ├── fitb
│ ├── login
│ ├── manage.py
│ ├── poetry.lock
│ ├── pyproject.toml
│ ├── requirements.txt
│ ├── static
│ └── templates
├── test_sample.py
├── tests
│ └── context.py
├── tox.ini
├── zero1.sh
└── zero_out.sh
我的 setup.py
文件如下:
from setuptools import setup
setup(
name='fitbapp',version='1.0',packages=['mypkg'],package_dir = {'mypkg': 'src/mypkg'},description='FITB app',long_description='App to generate fill in the blanks type questions from sentences',classifiers=['Programming Language :: Python'],py_modules= ['manage'],#install_requires = [ 'docutils','numpy' ],package_data={'mypkg': ['fitb/*','api/*','api/migrations/*.py','static/*.css','templates/*.html','templates/registration/*.html','login/*','QuestionGeneration/*','fitb/migrations/*.py','templatetags/*.py','data/pickles/nb-predictor.pkl'] },entry_points = {
'console_scripts' : ['manage-server=mypkg.manage:main'],}
#data_files = [("/",['static/static.css','src/templates/*.html','src/templates/registration/login.html'])]
)
并且我的 entry_point 被指定为 manage-server,当我使用 build
包使用 - python -m build
执行构建命令并安装 wheel file
时dist 目录,我可以使用 manage-server
访问它,输出如下所示:
Type 'manage-server help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[rest_framework]
generateschema
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
但是当我使用以下方法以可编辑模式安装它时:
$python install --editable .
我安装成功,但在尝试运行 manage-server
(即 manage.py
django 文件)时出现以下堆栈跟踪:
Traceback (most recent call last):
File "/Users/apple/.pyenv/versions/demo2/bin/manage-server",line 33,in <module>
sys.exit(load_entry_point('fitbapp','console_scripts','manage-server')())
File "/Users/apple/.pyenv/versions/demo2/bin/manage-server",line 25,in importlib_load_entry_point
return next(matches).load()
File "/Users/apple/.pyenv/versions/3.7.5/envs/demo2/lib/python3.7/site-packages/importlib_Metadata/__init__.py",line 105,in load
module = import_module(match.group('module'))
File "/Users/apple/.pyenv/versions/3.7.5/lib/python3.7/importlib/__init__.py",line 127,in import_module
return _bootstrap._gcd_import(name[level:],package,level)
File "<frozen importlib._bootstrap>",line 1006,in _gcd_import
File "<frozen importlib._bootstrap>",line 983,in _find_and_load
File "<frozen importlib._bootstrap>",line 953,in _find_and_load_unlocked
File "<frozen importlib._bootstrap>",line 219,in _call_with_frames_removed
File "<frozen importlib._bootstrap>",line 965,in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mypkg'
我不明白为什么会发生这种情况,而且它也妨碍了我在 editable
模式下的发展。
我的manage-server
脚本如下:
#!/usr/bin/env bash
set -e
[ -n "$PYENV_DEBUG" ] && set -x
program="${0##*/}"
if [[ "$program" = "python"* ]]; then
for arg; do
case "$arg" in
-c* | -- ) break ;;
*/* )
if [ -f "$arg" ]; then
export PYENV_FILE_ARG="$arg"
break
fi
;;
esac
done
fi
export PYENV_ROOT="/Users/apple/.pyenv"
exec "/Users/apple/.pyenv/libexec/pyenv" exec "$program" "$@"
在 /Users/apple/.pyenv/shims/manage-server
,我使用的是 macOS Catalina。我在 ModuleNotFoundError when executing my setuptools console_script 看到了一个有点类似的问题,但没有答案,而且我发现我的问题更加详细和不同,因为使用了 editable 模式和最新的 setuptools 文档说明。
我选择了 src/mypkg 布局来将我的模板和静态文件添加到滚轮 使用 mypkg
的 package_data
kwarg 将 setup.py
转换为包的文件。
[编辑] - 我测试了 console_scripts 是否可以在由 pip install -e .
创建的开发环境中工作,结果证明确实如此。一个最小的可重现示例如下:
setuptools: entry_points example link
entry-points using setup.py file
这个项目目录的目录结构如下:
$tree
.
├── pyproject.toml
├── setup.py
└── timmins
├── __init__.py
└── __main__.py
1 directory,4 files
对应的pyproject.toml文件如下:
[build-system]
requires = ["setuptools","wheel"]
build-backend = "setuptools.build_Meta"
而对应的setup.py文件如下:
from setuptools import setup
setup(
name='testapp',packages=['timmins'],entry_points = {
'console_scripts': ['hello-world = timmins:hello_world'],},)
在使用 pip install -e .
安装软件包并运行 hello-world
时,我得到以下预测输出:
Hello world
因此 console_scripts 也应该在开发环境中工作!
解决方法
我认为您的 package_dir
设置不正确,这会影响其余部分
应该是
package_dir={'': 'src'},
你也不应该有src/__init__.py