How to add a library in python urllib2 - python

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!!

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

Python import issues

I have never used Python before and am trying learn it on the fly. I have this file. I installed python 2.7 have added it the path. when I try python [filename].py I get errors saying
import requests,sys,csv,os,re,urllib2,urllib,time,traceback,string
ImportError: No module named requests.
Am I missing something here. Where would I find these and how do I get this running
You need to install the requests package:
pip install requests
Tutorial about installing packages here

No module named request PythonAnywhere error

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)

Getting error while using pip

I am trying to install BeautifulSoup to my Python 3.4 on my Windows 8.1. I have already set the PATH to pip folder. Then I run this command:
pip install BeautifulSoup
but i get this error:
How can i fix that?
This is because BeautifulSoup package maps to BeautifulSoup 3rd version which is not maintained anymore and doesn't support Python3. Quote from it's PyPI page:
This package is OBSOLETE. It has been replaced by the beautifulsoup4
package. You should use Beautiful Soup 4 for all new projects.
You need to install the beautifulsoup4:
pip install beautifulsoup4

Categories

Resources