简体   繁体   English

手动安装Python package

[英]Manual installation of Python package

I wrote a small python program and now I want to install and use it on my system.我写了一个小的 python 程序,现在我想在我的系统上安装和使用它。

Program structure:程序结构:

projectname
|--src
|  |--main.py #import file1
|  |--file1.py #import file2
|  |--file2.py
|--LICENSE
|--README.md
|--setup.py

I did sudo python setup.py install , but now I only able to run it with /usr/bin/main.py .我做了sudo python setup.py install ,但现在我只能用/usr/bin/main.py运行它。 What should I do, to be able to run it by only typing a projectname ?我应该怎么做才能通过只输入projectname来运行它?

EDIT: Setup.py content:编辑:Setup.py 内容:

setuptools.setup(
    name="projectname",
    version="0.0.1",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    entry_points={
        "console_scripts": [
            "projectname = projectname.src/main:main"
        ]
    },
    python_requires='>=3.6',
)

You can use setuptools' entry points in your setup.py.您可以在 setup.py 中使用 setuptools 的入口点

Example from the documentation:文档中的示例:

setup(
    # other arguments here...
    entry_points={
        "console_scripts": [
            "foo = my_package.some_module:main_func",
            "bar = other_module:some_func",
        ],
        "gui_scripts": [
            "baz = my_package_gui:start_func",
        ]
    }
)

With your structure, create file src/__init__.py :使用您的结构,创建文件src/__init__.py

from . import main

and lets say that this is src/main.py :让我们说这是src/main.py

def main():
    print("Hello world")

Finally, setup.py can be:最后, setup.py可以是:

import setuptools

setuptools.setup(
    name="projectname",
    version="0.0.1",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    entry_points={
        "console_scripts": [
            "projectname = src:main.main"
        ]
    },
    python_requires='>=3.6',
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM