如何正确打包可调用的python脚本或模块集

我已经在网上搜索了很长一段时间,但我似乎无法理解如何为我的最终用户分发我的python脚本。


我一直在使用此命令在命令行上使用脚本python samplemodule.py "args1"


这也是我希望我的用户也通过命令行使用它的方式。但我担心的是,这些模块依赖于其他库或模块。


当我的脚本都位于项目的根目录中时,它们正在工作,但是当我尝试将它们打包并将它们放在子目录中时,一切都会崩溃。


这方面的一个例子是,我现在无法运行我的脚本,因为当我从子目录导入模块时,脚本出错。data


这是我的项目结构。


MyProject

    \formatter

      __init__.py

      __main__.py

      formatter.py

      addfilename.py

      addscrapertype.py

     ...\data

         __init__.py

         helper.py

     csv_formatter.py

     setup.py

csv_formatter.py文件只是一个用于调用格式化程序.main 的包装器。


更新:我现在能够生成一个tar.gz包,但该包在我的计算机上安装时不可调用。


这是 setup.py:


import setuptools


with open("README.md", "r") as fh:

    long_description = fh.read()


setuptools.setup(

    name="formatter",

    version="1.0.1",

    author="My Name",

    author_email="sample@email.com",

    description="A package for cleaning and reformatting csv data",

    long_description=long_description,

    long_description_content_type="text/markdown",

    url="https://github.com/RhaEL012/Python-Scripts",

    packages=["formatter"],

    include_package_data=True,

    package_data={

    # If any package contains *.txt or *.rst files, include them:

        "": ["*.csv", "*.rst", "*.txt"],

    },

    entry_points={

         "console_scripts": [

            "formatter=formatter.formatter:main"

        ]

    },

    classifiers=[

        "Programming Language :: Python :: 3",

        "License :: OSI Approved :: MIT License",

        "Operating System :: OS Independent",

    ],

    python_requires='>=3.6',

    install_requires=[

        "pandas"

    ]

)

现在,在计算机上安装软件包后,我无法调用该模块,并导致错误:


Z:\>addfilename "C:\Users\Username\Desktop\Python Scripts\"

http://img2.mukewang.com/63296fe60001409509700290.jpg

更新:我尝试在虚拟环境中安装 setup.py,只是为了查看错误来自何处。

我安装它,然后我得到以下错误:FileNotFoundError: [Errno 2] no such file or directory: 'README.md'

我试图包括进来,但仍然没有运气。所以我试着把它做成一个字符串,只是为了看看安装是否会继续。README.mdMANIFEST.in

安装继续,但话又说回来,我遇到一个错误,说package directory 'formatter' does not exist


慕尼黑5688855
浏览 190回答 3
3回答

慕容3067478

由于我无法查看您的特定文件,因此我将仅解释我通常如何解决此问题。这就是我通常设置命令行界面(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__.pydef 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')但我认为您也可以将您想要的内容导入到 中,并且仍然以我在 中的方式引用它。在我们有:__init__.pysetup.pysetup.pyimport setuptoolssetuptools.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        },)现在安装软件包,然后如果它已安装,您可以调用: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 等。

慕田峪7331174

我不同意其他答案。您不应在__init__.py中运行脚本,而应在__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 而不是手动设置包。现在,您可以像以前一样运行来安装软件包。pip install ./完成运行后:.它运行您在 CLI 文件夹(或您调用的任何内容)中创建的__main__.py文件。python -m formatter.cli arguments关于打包模块的一个重要注意事项是,您需要使用相对导入。例如,您将在其中使用。如果要从相邻文件夹导入某些内容,则需要两个点。from .your_class_module import YourClassModule__init__.pyfrom ..helpers import HelperClass

犯罪嫌疑人X

我不确定这是否有帮助,但通常我使用轮包打包我的python脚本:pip install wheel python setup.py sdist bdist_wheel完成这两个命令后,将在“dist”文件夹中创建一个whl包,然后您可以将其上传到PyPi并从那里下载/安装,也可以使用“pip install ${PackageName}.py”离线安装它。这是一个有用的用户指南,以防万一有我没有解释的其他事情:https://packaging.python.org/tutorials/packaging-projects/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python