Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 确定Ipython是否正在虚拟环境中运行_Python 3.x_Ipython_Python Venv - Fatal编程技术网

Python 3.x 确定Ipython是否正在虚拟环境中运行

Python 3.x 确定Ipython是否正在虚拟环境中运行,python-3.x,ipython,python-venv,Python 3.x,Ipython,Python Venv,我已经通过发行版的软件包管理器在全球范围内安装了IPython。当我从虚拟环境中启动它时,我会收到一条消息UserWarning:尝试在虚拟环境中工作。我尝试使用florisla on描述的方法来判断它是否正常工作。我实际运行的代码经过轻微修改,可以在运行时打印出所有布尔值: # 1) VIRTUAL_ENV environment variable # Both 'virtualenv' and 'venv' set the environment variable '$VIRTUAL_ENV

我已经通过发行版的软件包管理器在全球范围内安装了IPython。当我从虚拟环境中启动它时,我会收到一条消息
UserWarning:尝试在虚拟环境中工作。我尝试使用florisla on描述的方法来判断它是否正常工作。我实际运行的代码经过轻微修改,可以在运行时打印出所有布尔值:

# 1) VIRTUAL_ENV environment variable
# Both 'virtualenv' and 'venv' set the environment variable '$VIRTUAL_ENV' when activating an environment. See PEP 405.
# You can read out this variable in shell scripts, or use this Python code to determine if it's set.
import os
running_in_virtualenv = "VIRTUAL_ENV" in os.environ
# alternative ways to write this, also supporting the case where
# the variable is set but contains an empty string to indicate
# 'not in a virtual environment':
running_in_virtualenv1a = bool(os.environ.get("VIRTUAL_ENV"))
running_in_virtualenv1b = bool(os.getenv("VIRTUAL_ENV"))
# The problem is, this only works when the environment is activated by the 'activate' shell script.
# You can start the environment's scripts without activating the environment, so if that is a concern, you have to use a different method.
print("1a:", running_in_virtualenv1a)
print("1b:", running_in_virtualenv1b)

# 2) sys.real_prefix and sys.base_prefix
# Virtualenv points 'sys.prefix' to the Python installed inside of the virtualenv as you would expect.
# At the same time, the original value of 'sys.prefix' is also made available as a new attribute of 'sys': 'sys.real_prefix'. This is what we're using to detect if we're in a virtualenv.
import sys
running_in_virtualenv = hasattr(sys, "real_prefix")
# There's a problem though: 'venv' and 'pyvenv' do this differently from 'virtualenv' -- they don't set 'real_prefix'. Instead, they set 'base_prefix' to a different value from 'sys.prefix'.
# So to be safe, check both as suggested in hroncok's answer:
import sys
virtualenv_prefix = getattr(sys, "real_prefix", None)
venv_prefix = getattr(sys, "base_prefix", sys.prefix)
running_in_virtualenv2 = virtualenv_prefix or venv_prefix != sys.prefix
print("2:", running_in_virtualenv2)
现在,如果我在虚拟环境之外运行这段代码,我会得到三个
False
,分别使用
python3
shell和
ipython3
,但是如果我在虚拟环境中,IPython会为最后一个布尔值返回
False
running\u in\u virtualenv2
),而
python3
则为所有这些函数返回
True


我是否仍然可以使用IPython在虚拟环境中进行修补,而不必冒着弄乱系统安装的风险?

虚拟环境主要是关于修改导入搜索路径

import pprint
import sys

pprint.pprint(sys.path)
检查不同环境中的
路径
, 并选择与您的venv相对应的目录元素, 例如,当您 键入
$whichpython
。 让您的代码根据状态更改其行为
pprint.pprint(sys.path)
在列表顶部有
/home/arch/src/python/scratch/.scrvenv/lib/python3.7/site packages
,如果从
ipython3
运行,则在列表底部。那是不是意味着我很安全?(我正试图让事情变得简单,因为我刚刚开始学习Python。我想知道修补程序包是否安全。破坏脚本无关紧要,但我宁愿不破坏整个系统。)