1

I need to use urllib module in my code and I import it like this:

import urllib.request
import urllib.error

but PythonAnywhere returns the following error:

> No module named request

It looks like the urllib library is imported successfully when I try:

python3 myscript.py

instead of:

python myscript.py

But in this case I get another error:

> No module named 'pyvirtualdisplay'

Pyvirtualdisplay is also needed in my code, so I dont know what to do. Can someone help ?

Steve
  • 722
  • 4
  • 10
  • 24

3 Answers3

3

The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error ~ urllib2 - python docs

When you run your script using

python myscript.py

Your system is using python2 which doesn't have the urllib.request and urllib.error modules. Use the urllib2 library.

francium
  • 2,384
  • 3
  • 20
  • 29
2

You need to install Python extensions into each copy of Python that you use. For example, python and python3 use different set of extensions. You may have a script called pip3 that installs extensions into your copy of Python 3.

Installation instructions on the PyVirtualDisplay project page state that first you'll need to install pip and Pillow for Python 3. If you were using a Debian or Ubuntu VPS, these might work in a terminal:

sudo apt-get update
sudo apt-get install python3-pip python3-imaging
sudo pip3 install pyvirtualdisplay

But a Google search tells me PythonAnywhere is a web application hosting service. The list of supported extensions includes pyvirtualdisplay in Python 2 but not in Python 3. Just a guess, but the administrators may not be aware that pyvirtualdisplay has been ported. I'd recommend contacting PythonAnywhere support and requesting the extension's installation into Python 3.

Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64
1

You can install pyvirtualdisplay yourself for python 3. Either use a virtualenv (there are details on the help pages) or use the --user argument to pip and ensure you use the correct version of pip (pip3.3 or pip3.4 depending on the version you want to use)

Glenn
  • 7,262
  • 1
  • 17
  • 23