python virtualenv:为什么我仍然可以在干净/新的 virtualenv 中导入旧模块 [英] python virtualenv: why can I still import old modules in clean/new virtualenv

查看:39
本文介绍了python virtualenv:为什么我仍然可以在干净/新的 virtualenv 中导入旧模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始一个新的 Python 项目,我想使用 virtualenv 来有一个干净的开始.设置好之后,我仍然可以导入我没有为新的 virtualenv 安装的旧模块 - 为什么?

I'm starting a new Python project, and I want to work with virtualenv to have a clean start. After setting one up though, I can still import old modules that I have not installed for the new virtualenv - why?

我创建了一个:

virtualenv ~/virtualenvs/mynewproject --no-site-packages

现在我用 source bin/activate

现在,当我启动一个 python 解释器时(只需输入 python),我认为它会在我的 virtualenv 中使用 python 解释器,而我的 pythonpath 将被设置为 site-packages 路径我的 virutalenv 的 python (/virtualenvs/mynewporject/lib/python2.7/site-packages),没有别的.

Now when I start a python interpreter (by just typing python), I thought that it will use the python interpreter in my virtualenv, and my pythonpath would have been set to the site-packages path of my virutalenv's python (/virtualenvs/mynewporject/lib/python2.7/site-packages), and nothing else.

然而,当我查看 sys.path 时,所有旧的、系统范围的包都可用,我可以很好地导入它们 - 这是我不想要的.

However, when I look at sys.path, all the old, system-wide packages are available, and I can import them fine - which is what I don't want.

我在这里遗漏了什么?

推荐答案

检查您的 PYTHONPATH 环境变量,该变量可能指向您拥有旧版本软件包的位置.此变量始终位于您的 sys.path 中,因此请确保您清除它或将其更改为指向您激活的 virtualenv.

Check your PYTHONPATH environment variable which probably points to where you have the older version of your package. This variable always comes first in your sys.path so make sure you either clear it or change it to point to the virtualenv which you activate.

这篇关于python virtualenv:为什么我仍然可以在干净/新的 virtualenv 中导入旧模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆