我的urllib3
虚拟环境中安装了sampler_env
。
我在此virtualenv中还有另一个具有pytube
依赖项的库urllib
,例如:
import urllib2
from urllib import urlencode
有时候pytube
会引发错误,我可以在回溯中看到它使用系统urllib
,而不是virtualenv
内部的系统:
Traceback (most recent call last):
File "/home/bart/python_projects/sampler_env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/bart/python_projects/sampler_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/bart/python_projects/sampler_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/bart/python_projects/sampler_env/sampler_proj/sampler/views.py", line 44, in download_audio
audio_manager.download()
File "/home/bart/python_projects/sampler_env/sampler_proj/sampler/utils/audio_manager.py", line 163, in download
self.audio.download(dst, self.video_title)
File "/home/bart/python_projects/sampler_env/lib/python3.5/site-packages/pytube/streams.py", line 217, in download
bytes_remaining = self.filesize
File "/home/bart/python_projects/sampler_env/lib/python3.5/site-packages/pytube/streams.py", line 164, in filesize
headers = request.get(self.url, headers=True)
File "/home/bart/python_projects/sampler_env/lib/python3.5/site-packages/pytube/request.py", line 21, in get
response = urlopen(url)
# why system urllib?
File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.5/urllib/request.py", line 472, in open
response = meth(req, response)
File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.5/urllib/request.py", line 510, in error
return self._call_chain(*args)
File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
result = func(*args)
File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
我该如何解决?