No module named request PythonAnywhere error - python

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 ?

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.

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.

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)

Related

ModuleNotFoundError: No module named '...'

I'm trying to install a few packages (beautifulsoup4 and requests) for a project.
I installed these packages by running these commands in the terminal on macOS:
pip3 install beautifulsoup4
pip3 install requests
both did install successfully.
Now if I open my project in PyCharm, I can import the modules by using:
from bs4 import BeautifulSoup
import requests
Both packages are imported successfully and can be used in my code.
To be sure everything was installed correctly, I looked at the venv/lib/Python3.10 folder. beautifulsoup4-4.10.0.dist-info, bs4, requests and requests-2.27.1.dist-info are present.
However, when I run the CGI script (Python), I get the following error in the terminal:
from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
Even when I open a new terminal window and run the following commands:
python3
>>> from bs4 import BeautifulSoup
it runs fine without any errors.
The same issue happens for the requests package.
I also ran pip3 install bs4 but that also didn't fix anything.
I found what caused the issue. bs4 and requests were both installed correctly but in my CGI script, I used the wrong Python path. I had to change #!/usr/bin/python3 to the path that which python3 gave me.
I would recommend to always install packages using python3 -m pip install <name_of_the_package>. In this way you can be sure that you are using the correct pip.
To make sure that the python you a using really has the packages installed, try python3 -m pip list.
If all is good, check if the python interpreter the pycharm is using in your project is the right one.
It sounds to me that in pycharm you're using a virtual environment, but when you are using python3 at the terminal, you a using the main installation.

How do I enable Python requests?

I am a PHP developer, tasked to write Python scripts.
The script uses requests, which I found to be the easiest Python equivalent to PHP cURL.
If you use cURL in PHP, cURL must be enabled on the server, otherwise, the script won't work.
Is there likewise any activation or enabling necessary in Python to use requests other than import requests?
You must also have pip installed on your computer to install the requests module, pip comes installed with python 3.4 and above so you could do something like "pip install requests"
Install requests on machine you are going to use it: pip install requests.
Note if you are on linux you might have 2 versions of python installed: python 2.* and python 3.*
If this is the case using pip install <package_name> will install it for python 2.*, so you should use pip3 install <package_name> to install it for python 3.
(I would also suggest using virtualenv, but this is another topic).
If package is installed then you can use it in your scripts by simply importing it: import requests
Nothing more is needed to enable/activate libraries(packages). It is the case for all packages in python. (install -> import)
Try this steps in order to use requests
If in Linux, you can $ pip install request from the terminal or $ pip3 install requests if you are using python3
Then, inside python interpreter,
import requests
url = "https://google.com"
res = requests.get(url)
print(res.status_code)
This will give you a 200 code

Getting No module named 'requests' with jodogne/orthanc-python

I am trying to extend the orthanc server with few custom code refering to https://book.orthanc-server.com/plugins/python.html#auto-routing-studies . I am having my custom code written into python script and its getting picked up aswell.
The issue is as mentioned in the document when i try to use requests module with the latest jodogne/orthanc-python image it gives me ModuleNotFoundError .
I tried installing requests library separately but its not getting picked up by my code.
Any help would really be appreciated.
I also encountered issues when using the latest jodogne/orthanc-python image. I built a version that is able to use pip install. You can try out the Dockerfile below:
FROM j3soon/orthanc-python:1.9.6
RUN apt-get update && apt-get install -y python3-pip
RUN python3.7 -m pip install requests
Alternatively, launch j3soon/orthanc-python:1.9.6 directly and run the commands above.
First you need to install the requests library with pip, then you have to import requests inside your Python script.

Trying to install a pack from source. I get: ImportError: No module named requests Yet the pack is installed

I'm trying to install: https://github.com/rozzac90/pinnacle.git from source.
If I type sudo python setup.py install (or sudo python setup.py configure) I get:
import requests
ImportError: No module named requests
The module is installed, I'm sure. It is in the list of conda installed packs. Also, if I try to import it in other scripts, and
python
>>> import requests
>>>
with both python 2 and python3. I get no errors.
I also removed it and installed it again. Same results.
I am using Anaconda with a python3 environment. (I get the same errors in python 2, though.) So I can't use pip commands. I'm running a lubuntu machine, latest version.
Ideas anyone? Thanks in advance !
A suggested answer :
The solution was to run the script in the folder where setup.py is located, in general :
Try with
sudo /[annaconda_python_path]/python setup.py install
Make sure the right environment is active in the terminal.

How to add a library in python urllib2

I am using python ver 3.3. I am trying to access the requests.get() method. But I am not able to access this because in my
import requests
It is showing module not found. How can I install this for the code to work properly?
install it from pypi repository using pip:
pip install requests
Don't forget to be root or admin!!

Categories

Resources