making a commandline alias to a python file in a pipenv project

后端 未结 3 1406
谎友^
谎友^ 2021-01-06 21:01

I\'ve been making a python project using pipenv, and I want to be able to run it in a terminal from any location on my (linux) system. Specifically, say I have the following

3条回答
  •  日久生厌
    2021-01-06 21:35

    You should use the standard setuptools library to write a setup.py file. In particular you can write an entry_points section that names your main script:

    entry_points={
      'console_scripts': [
        'alias_to_my_project = project.main.main'
      ]
    }
    

    Once you've done this, you can activate and install your package into your virtual environment

    pipenv install -e .
    
    # or without pipenv
    . ~/vpy/bin/activate
    pip install -e .
    

    This will create a wrapper script in $VIRTUAL_ENV/bin/alias_to_my_project that loads the project.main Python module and calls its main function.

    The wrapper script knows about the virtual environment and can be called directly without specifically activating the virtual environment. So you can do something like

    ln -s $VIRTUAL_ENV/bin/alias_to_my_project $HOME/bin/alias_to_my_project
    PATH=$HOME/bin:$PATH
    

    and it will always be available.

提交回复
热议问题