[英]Install module to site-packages with setuptools
I'm trying to upload a CLI to PIP that, once installed, will run when the user types myscript
我正在尝试将CLI上传到PIP,一旦安装,将在用户键入
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
My setup.py
should install the lib.myscript
package and install myscript
as a command-line module 我的
setup.py
应该安装lib.myscript
包并安装myscript
作为命令行模块
setup.py
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
Once I do python setup.py install
, myscript
is installed as a command-line module and it runs. 一旦我做了
python setup.py install
, myscript
就会作为命令行模块安装并运行。 However, it throws an error saying that there is no module named myscript
. 但是,它会抛出一个错误,说没有名为
myscript
模块。
You haven't install myscript
, you've installed lib.myscript
so try this: python -m lib.myscript
. 你还没有安装
myscript
,你已经安装了lib.myscript
所以试试这个: python -m lib.myscript
。 And for Python to recognize lib
as a package create an empty file lib/__init__.py
. 并且让Python将
lib
识别为一个包创建一个空文件lib/__init__.py
。
PS. PS。 This code:
这段代码:
#!/usr/bin/env bash
if [[ ! $@ ]]; then
python -m myscript -h
else
python -m myscript $@
fi
could be simplified as: 可以简化为:
#!/usr/bin/env bash
exec python -m myscript ${@:--h}
which in shell-speak means "Use $@
if not empty, else -h
" 在shell-speak中表示“如果不为空则使用
$@
,否则-h
”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.