3 回答

TA貢獻1773條經(jīng)驗 獲得超3個贊
由于我無法查看您的特定文件,因此我將僅解釋我通常如何解決此問題。
這就是我通常設(shè)置命令行界面(cli)工具的方式。項目文件夾如下所示:
Projectname
├── modulename
│ ├── __init__.py # this one is empty in this example
│ ├── cli
│ │ ├── __init__.py # this is the __init__.py that I refer to hereafter
│ ├── other_subfolder_with_scripts
├── setup.py
其中所有功能都在模塊名稱文件夾和子文件夾中。在我的我有:__init__.py
def main():
# perform the things that need to be done
# also all imports are within the function call
print('doing the stuff I should be doing')
但我認為您也可以將您想要的內(nèi)容導(dǎo)入到 中,并且仍然以我在 中的方式引用它。在我們有:__init__.pysetup.pysetup.py
import setuptools
setuptools.setup(
name='modulename',
version='0.0.0',
author='author_name',
packages=setuptools.find_packages(),
entry_points={
'console_scripts': ['do_main_thing=modulename.cli:main'] # so this directly refers to a function available in __init__.py
},
)
現(xiàn)在安裝軟件包,然后如果它已安裝,您可以調(diào)用:pip install "path to where setup.py is"
do_main_thing
>>> doing the stuff I should be doing
對于我使用的文檔:https://setuptools.readthedocs.io/en/latest/。
我的建議是從這里開始,慢慢添加你想要的功能。然后一步一步地解決你的問題,比如添加 README.md 等。

TA貢獻1828條經(jīng)驗 獲得超13個贊
我不同意其他答案。您不應(yīng)在__init__.py中運行腳本,而應(yīng)在__main__.py中運行腳本。
Projectfolder
├── formatter
│ ├── __init__.py
│ ├── cli
│ │ ├── __init__.py # Import your class module here
│ │ ├── __main__.py # Call your class module here, using __name__ == "__main__"
│ │ ├── your_class_module.py
├── setup.py
如果您不想提供自述文件,只需刪除該代碼并手動輸入說明即可。
我使用 https://setuptools.readthedocs.io/en/latest/setuptools.html#find-namespace-packages 而不是手動設(shè)置包。
現(xiàn)在,您可以像以前一樣運行來安裝軟件包。pip install ./
完成運行后:.它運行您在 CLI 文件夾(或您調(diào)用的任何內(nèi)容)中創(chuàng)建的__main__.py文件。python -m formatter.cli arguments
關(guān)于打包模塊的一個重要注意事項是,您需要使用相對導(dǎo)入。例如,您將在其中使用。如果要從相鄰文件夾導(dǎo)入某些內(nèi)容,則需要兩個點。from .your_class_module import YourClassModule__init__.pyfrom ..helpers import HelperClass

TA貢獻2080條經(jīng)驗 獲得超4個贊
我不確定這是否有幫助,但通常我使用輪包打包我的python腳本:
pip install wheel python setup.py sdist bdist_wheel
完成這兩個命令后,將在“dist”文件夾中創(chuàng)建一個whl包,然后您可以將其上傳到PyPi并從那里下載/安裝,也可以使用“pip install ${PackageName}.py”離線安裝它。
這是一個有用的用戶指南,以防萬一有我沒有解釋的其他事情:
https://packaging.python.org/tutorials/packaging-projects/
添加回答
舉報