简体   繁体   English

Python Virtualenv检查环境

[英]Python Virtualenv Check Environment

I am writing a bash script which need to start a service using virtualenv . 我正在编写一个bash脚本,需要使用virtualenv启动服务。 I imagine my bash script to look like this: 我想我的bash脚本看起来像这样:

#!/bin/bash
source /root/user/python-ev/bin/activate
IPYTHON_OPTS="notebook --port 8889 --profile pyspark" pyspark --master spark://701.datafireball.com:7077

However, I want to add a line between activate and IPYTHON... to make sure that the environment has been activated. 但是,我想在activateIPYTHON...之间添加一行IPYTHON...以确保环境已被激活。 Is there an environment variable / command in the shell that can tell me if I am inside a virtualenv or not, if so, which one? shell中是否有环境变量/命令可以告诉我是否在virtualenv中,如果是,是哪一个?

I can hard code to print the python path, where it should point to a customized python interpreter if I am inside the virtualenv, but I am not sure if that is the proper way to do it. 我可以硬编码打印python路径,如果我在virtualenv中,它应该指向一个自定义的python解释器,但我不确定这是否是正确的方法。

Thanks! 谢谢!

You can check for the environment variable VIRTUAL_ENV and see if it has the correct source path. 您可以检查环境变量VIRTUAL_ENV并查看它是否具有正确的源路径。 Now if it doesn't exist then you know it's not activated. 现在,如果它不存在,那么你知道它没有被激活。 If it does, you need to check and see if it has the correct path. 如果是,则需要检查并查看其是否具有正确的路径。

The correct bash snippet that will work to check if the variable is set or not is the following 正确用于检查变量是否设置的正确bash片段如下

if [[ -z "$VIRTUAL_ENV" ]]; then
    echo "No VIRTUAL_ENV set"
else
    echo "VIRTUAL_ENV is set"
fi

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

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