使用setuptools将模块安装到站点软件包 [英] Install module to site-packages with setuptools
问题描述
我正在尝试将CLI上载到PIP,一旦安装,它将在用户键入myscript
I'm trying to upload a CLI to PIP that, once installed, will run when the user types myscript
我的文件夹结构是这样的:
My folder structure is like this:
lib
myscript
__init__.py (empty)
__main__.py (code that needs to run)
utilities.py (needs to be imported from main)
scripts
myscript
setup.py
我的setup.py
应该安装lib.myscript
软件包并将myscript
作为命令行模块安装
My setup.py
should install the lib.myscript
package and install myscript
as a command-line module
import setuptools
setuptools.setup(
name='myscript',
version='1.0',
scripts=['./scripts/myscript'],
packages=['lib.myscript'],
install_requires=['setuptools', 'pandas >= 0.22.0', 'numpy >= 1.16.0'],
python_requires='>=3.5'
)
scripts/myscript
#!/usr/bin/env bash
if [[ ! $@ ]]; then
python -m myscript -h
else
python -m myscript $@
fi
一旦我执行了python setup.py install
,就会将myscript
作为命令行模块安装并运行.但是,它抛出一个错误,指出没有名为myscript
的模块.
Once I do python setup.py install
, myscript
is installed as a command-line module and it runs. However, it throws an error saying that there is no module named myscript
.
推荐答案
您尚未安装myscript
,已经安装了lib.myscript
,请尝试以下操作:python -m lib.myscript
.为了让Python将lib
识别为包,请创建一个空文件lib/__init__.py
.
You haven't install myscript
, you've installed lib.myscript
so try this: python -m lib.myscript
. And for Python to recognize lib
as a package create an empty file lib/__init__.py
.
PS.这段代码:
#!/usr/bin/env bash
if [[ ! $@ ]]; then
python -m myscript -h
else
python -m myscript $@
fi
可以简化为:
#!/usr/bin/env bash
exec python -m myscript ${@:--h}
在shell说话中表示如果不为空,则使用$@
,否则为-h
"
which in shell-speak means "Use $@
if not empty, else -h
"
这篇关于使用setuptools将模块安装到站点软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!