简体   繁体   中英

How can I run a Python pipenv from another directory?

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 ?

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.)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM