简体   繁体   English

如何从另一个目录运行 Python pipenv?

[英]How can I run a Python pipenv from another directory?

I have a project for which I'd now like to use pipenv.我有一个项目,现在我想使用 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.我想从我的主 bin 目录符号链接它,所以我可以从另一个目录(它与本地文件交互的地方)运行它,但仍然使用适当安装的文件在 pipenv 中运行它。

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 ?或者它不会选择 /PATH/TO/MY/CODE/Pipenv 中定义的 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.不确定这是否与您在 2018 年使用的pipenv版本相关,但从当前版本开始,您可以使用PIPENV_PIPFILE环境变量。 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. pipenv 是 virtualenv 的包装器,它将 virtualenv 文件保存在主目录的某个文件夹中。 I found them in /home/MYUSERNAME/.local/share/virtualenvs .我在/home/MYUSERNAME/.local/share/virtualenvs找到了它们。

So i wrote a small_script.sh :所以我写了一个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.它为自己激活 virtualenv,然后在您的本地目录中运行您的 python 代码,最后停用 virtualenv。

You can replace localfile.conf by $@ which allows you to run ./small_script.sh localfile.conf .您可以用$@替换localfile.conf ,这允许您运行./small_script.sh localfile.conf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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