Asaf Nevo
Asaf Nevo

Reputation: 11688

python create system alias to script

I creating an install script for my python project which installs all the external dependencies I need in order to run properly.

I want to create a system alias named myScript which will be alias for path/to/script/run.py so users could just run it by using myScript command

How can I do it?

Upvotes: 4

Views: 2002

Answers (2)

Brendan Abel
Brendan Abel

Reputation: 37549

If your project has a setup.py script and you're installing your python packages, scripts, and dependencies using setuptools (and you should be), you can use the entry_points feature of setuptools.

setup.py

from setuptools import setup

setup(
    # other arguments here...
    entry_points={
        'console_scripts': [
            'myScript = my_package.run:main',
        ],
    },
)

Your project structure should look like this:

setup.py
/my_package
    __init__.py
    run.py

Your run.py script should have a main() function, which will get run when someone types myScript at the command line. This works regardless of what shell they use or what platform you're on.

Upvotes: 5

wolendranh
wolendranh

Reputation: 4292

In terminal:

gedit ~/.bashrc

then inside .bashrc:

alias myScript ='python path/to/script/run.py'

reload bash, and it should work

To be sure where you are executing your script, to now which file to edit, you can check this with Python:

>>> import platform
>>> platform.system()
'Linux'
>>> 

To check if it is Mac:

platform.mac_ver()

Upvotes: 0

Related Questions