How do I install a script to run anywhere from the command line?

后端 未结 10 1122
梦谈多话
梦谈多话 2020-12-04 10:14

If I have a basic Python script, with it\'s hashbang and what-not in place, so that from the terminal on Linux I can run

/path/to/file/MyScript [args]
         


        
10条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 10:50

    you can also use setuptools (https://pypi.org/project/setuptools/)

    1. your script will be:
    def hi():
        print("hi")
    

    (suppose the file name is hello.py)

    1. also add __init__.py file next to your script (with nothing in it).

    2. add setup.py script, with the content:

    #!/usr/bin/env python3
    
    import setuptools
    
    install_requires = [
            'WHATEVER PACKAGES YOU NEED GOES HERE'
            ]
    
    setuptools.setup(
        name="some_utils",
        version="1.1",
        packages=setuptools.find_packages(),
        install_requires=install_requires,
        entry_points={
            'console_scripts': [
                'cool_script = hello:hi',
            ],
        },
        include_package_data=True,
        )
    
    
    1. you can now run python setup.py develop in this folder
    2. then from anywhere, run cool_script and your script will run.

提交回复
热议问题