Python 在 vi​​rtualenv 中使用 Urllib2 的任何地方问题

Python Anywhere issue using Urllib2 with virtualenv

我在 PythonAnywhere 上使用 Django 服务器,使用 virtualenv。在我使用抓取代码编写文本文件的服务器中,该代码使用 urllib2,默认情况下应捆绑在 python 中,但存在各种错误:

pip install urllib2

找不到满足 urllib2 要求的版本(来自版本:)
找不到 urllib2 的匹配分布

apt-get install python-urllib2

E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied) E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

问题的根源当然是这个(来自 python 控制台):

import urllib2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'urllib2'

在Python2中,urllib2是标准库的一部分。您不必使用 pip 或包管理器安装它,它已经是您 Python 2 安装的一部分。

pip 命令失败,因为没有要安装的外部包 urllib2。

apt-get 命令失败,因为您没有正确的权限。即使你使用 sudo 或切换到 root,它也会失败,因为 Python-urllib2 不存在。

如果您使用的是 Python 3,则 urllib2 模块不再存在,并且已拆分为 urllib.requesturllib.error。您需要更新代码才能使用 Python 中的新 urllib 模块 3.