简体   繁体   中英

Manual installation of Python package

I wrote a small python program and now I want to install and use it on my system.

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 . What should I do, to be able to run it by only typing a projectname ?

EDIT: Setup.py content:

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.

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 :

from . import main

and lets say that this is src/main.py :

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

Finally, setup.py can be:

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',
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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