0

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',
)
2
  • Make a script... Commented Jul 2, 2020 at 11:47
  • Make an alias on your machine? Commented Jul 2, 2020 at 11:48

1 Answer 1

1

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',
)
Sign up to request clarification or add additional context in comments.

4 Comments

So how should it be in my case? projectname = projectname.src/main:main gives me an error. I am a little unfamiliar with the syntax.
@Arden Can you please add the contents of setup.py to your original question?
I have a main function in the main file and also if __name__ == '__main__': curses.wrapper(main), if that is important.
@Arden First, you need to create file src/__init__.py with contents from . import main. Then, the setup line becomes "projectname = src:main.main". Also see github.com/pypa/sampleproject/blob/master/setup.py for an extensive example of a project. I would also advise you to rename the src folder because this is the module name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.