python - 导入错误 : cannot import name "urandom"

标签 python django virtualenv

当我在运行虚拟环境 (workon django1.2) 时尝试 runserver 我的任何项目时出现此错误

错误:

ImportError: cannot import name "urandom"

在研究了我发现的问题后 this :

Warning Python bugfix releases 2.6.8, 2.7.3, 3.1.5 and 3.2.3 include a change that will cause “import random” to fail with “cannot import name urandom” on any virtualenv created on a Unix host with an earlier release of Python 2.6/2.7/3.1/3.2, if the underlying system Python is upgraded. This is due to the fact that a virtualenv uses the system Python’s standard library but contains its own copy of the Python interpreter, so an upgrade to the system Python results in a mismatch between the version of the Python interpreter and the version of the standard library. It can be fixed by removing $ENV/bin/python and re-running virtualenv on the same target directory with the upgraded Python.

但是运行 ENV/bin/python 给了我:

bash: ENV/bin/python: No file or directory of this type

在哪里可以找到我的 virtualen 目录?

我对 virtualenvs 了解甚少,请多多包涵

谢谢

最佳答案

你必须重新创建你的虚拟环境。

我的猜测是发生了以下情况:

  1. 您创建了一个虚拟环境
  2. 在稍后的某个时间点,系统的 Python 安装更新了 urandom 安全错误修复。
  3. 您的 virtualenv(从以前的 Python 点版本创建)不再工作(由于您在问题中提到的问题)

最简单的解决方法是删除您的虚拟环境并创建一个新环境:

$ rm -r VIRTUALENVDIR
$ virtualenv VIRTUALENVDIR
$ . VIRTUALENVDIR/bin/activate

# then pip install any required packages, if your project has a requirements.txt file,
# this is simply:
$ pip install -r requirements.txt

# otherwise, you will have to install each package
$ pip install packagename
$ pip install packagename==version

关于python - 导入错误 : cannot import name "urandom",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13194308/

相关文章:

python - 在 virtualenv 中使用 pip 安装时出现“权限被拒绝”错误

python - 有没有办法将变量发送到特定任务?

python - 预期方法不起作用

python - 函数返回函数返回函数

python - Pipenv:多环境

python - Pip 识别 virtualenv 中链接的站 pip 包

python - PyMongo 连接被拒绝

python - 中大型站点的项目根目录中通常有十几个 django 应用程序吗?不觉得臃肿吗?

python - data 必须类似于 QuerySet(具有 count() 和 order_by())或支持 list(data) —— NoneType 两者都不具备

jquery - Django + jQuery : Why CSRF verification fails on multiple simultaneous requests