15

I have a project for which I'd now like to use pipenv.

I want to symlink this from my main bin directory, so I can run it from another directory (where it interacts with local files) but nevertheless run it in the pipenv with the appropriately installed files.

Can I do something like

pipenv run python /PATH/TO/MY/CODE/foo.py localfile.conf

Or is that not going to pick up the pipenv defined in /PATH/TO/MY/CODE/Pipenv ?

1

2 Answers 2

36

Not sure if this was relevant with the version of pipenv you used in 2018, but as of current versions you can use the PIPENV_PIPFILE environment variable. You will end up with a wrapper shell script that looks something like:

export PIPENV_PIPFILE=/my/project/dir/Pipfile
exec pipenv run command ...

(Answering even though I'm half a year late because this was the first relevant search result, so I can find it again next time.)

Sign up to request clarification or add additional context in comments.

3 Comments

Why is exec necessary? I tried it without exec and it worked just fine.
Shouldn't it work as PIPENV_PIPFILE=/my/project/dir/Pipfile pipenv run command?
@JamyMahabier With exec, the shell script gets terminated / replaced by the Python process. Without exec, the wrapper script will run until the Python script is terminated, which wastes memory, and can cause confusion when sending signals to either process.
5

pipenv is a wrapper for virtualenv which keeps the virtualenv-files in some folder in your home directory. I found them in /home/MYUSERNAME/.local/share/virtualenvs.

So i wrote a small_script.sh:

#!/bin/bash
source /home/MYUSERNAME/.local/share/virtualenvs/MYCODE-9as8Da87/bin/activate
python /PATH/TO/MY/CODE/foo.py localfile.conf
deactivate

It activates the virtualenv for itself, then runs your python code in your local directory and finally deactivates the virtualenv.

You can replace localfile.conf by $@ which allows you to run ./small_script.sh localfile.conf.

1 Comment

One downside of this is that any extra behaviour that pipenv has, e.g. automatically loading .env files, won't be performed. If you just need the right Python interpreter with the right dependencies it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.