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 directory structure:
/home
/project
Pipfile
main.py
/other_dir
I would like to be able to make an alias that allows me to call main.py
like so:
/home/other_dir$ alias_to_my_proyect --some args
and run it in the virtual env, having the same behaviour as
/home/project$ pipenv run python main.py
But in another directory.
If it weren't a pipenv project, I'd just use a shebang a the start of the file and then add an alias to it in my .bashrc, but I want to use pipenv's virtual environment, but I cant find a way to do this with pipenv.
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: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:
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: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 anentry_points
section that names your main script:Once you've done this, you can activate and install your package into your virtual environment
This will create a wrapper script in
$VIRTUAL_ENV/bin/alias_to_my_project
that loads theproject.main
Python module and calls itsmain
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
and it will always be available.
You can just use
in your script after you install my pipenv-shebang package: