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

久未见 提交于 2019-12-01 09:43:18

If you want to use a specific python environment for your script you will need to point it to the interpreter of that environment. On Mac the default is that pipenv installs all virtualenvs to /Users/<user_name>/.local/share/virtualenvs/ however that can be set to different locations as described in the manual:

Pipenv automatically honors the WORKON_HOME environment variable, if you have it set — so you can tell pipenv to store your virtual environments wherever you want, e.g.:

export WORKON_HOME=~/.venvs

In addition, you can also have Pipenv stick the virtualenv in project/.venv by setting the PIPENV_VENV_IN_PROJECT environment variable.

You can find out where the exact location of the virtualenv is with pipenv --venv inside your project folder. It returns something like /Users/reedef/.local/share/virtualenvs/project-BpR9WgCa. The interpreter is in ./bin/python of that location.

If we assume that you did not set any environment variable and you are using Mac than that means that you can write a script:

#!/usr/bin/env sh
/Users/reedef/.local/share/virtualenvs/project-BpR9WgCa/bin/python /home/project/main.py

and place it somewhere in your $PATH, e.g. /usr/local/bin/my_fancy_main to let it run in that specific environment.

Note: as mentioned by @Jon in the comments, -BpR9WgCa at the end of the path is stable as it is made from the project path:

hash = hashlib.sha256(location.encode()).digest()[:6]

It should be the same as long as the project path hasn't changed.

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.

You can just use

#!/usr/bin/env pipenv-shebang

in your script after you install my pipenv-shebang package:

pip install pipenv-shebang
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!