Im having trouble importing a the request module in Python - python

I wanted to use the request library for python and I went to windows powershell and put in 'pip install requests'and it said install successful but when I open up an ide it says that it cannot find the import when I try to use it. How do I use a python import installed with the pip command?

suppose virtual environment issue
to check:
under powershell: pip show requests
notice the path it installed
under IDE, open the shell and type: pip show requests
it should show nothing if you inside the virtual environment. then simply pip install requests again, and then pip show requests, you would see the requests module under your venv folder

Related

Requests module for python not being recognized

I am on a Mac.
I installed the requests module to do some web scraping but whenever I run the file it is telling me that it doesn't recognize the requests module. I then checked if the module is installed and it was. I've attached a screenshot.
And here is the error message
File "scraper.py", line 1, in <module>
import requests
ImportError: No module named requests
The issue is with the different versions of Python I have. Mac ships with 2.7 by defualt and even though I installed the requests module it was installed for the default python version.
So what I did was
Installed the latest Python version
Changed the default Interpreter Path in Vscode . Here is the official Vscode guide https://code.visualstudio.com/docs/python/environments
And finally installed requests for Python 3 using this command in the terminal
sudo python3 -m pip install requests
You may need to update your pip too
pip install --upgrade pip
I still think I need to manage the Python environments better but that is for another time.

Why do I receive a "no module named scipy" error even after I have installed the module via pip?

I am using Python 3.9 on Windows 10 which I downloaded directly from the Microsoft Store.
I tried running a script in PowerShell: Bash *.sh
This script is supposed to tell my computer to execute a .py script which uses scipy.io and many other modules.
Then I received this error:
ModuleNotFoundError: No module named 'scipy'
My strategy was to make sure pip was up to date, then use it to install the desired packages, then run some commands to see if the packages were installed.
I ran this command to update pip:
python3 -m pip install --upgrade pip
I ran this command to get some modules:
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
I also tried this command just in case:
pip install scipy
and got the result:
Requirement already satisfied ...
I ran the command pip list to make sure scipy was in the list (and it was there).
Then I ran the command python and my prompt changed to ">>>" and entered import scipy and did not receive any errors.
I am very confused as to how I have scipy installed yet have my script tell me it isn't there. Please help!
From what you have posted it looks like you have more than one python environment path in your system, because of which when you are installing these libraries they are installed at certain location while when you run the bash script it is using some other python location.
Try using these commands in both your terminal (cmd in windows) as well as in you bash script:
import sys
print(sys.path)
This will give you the python environment path (location where your python libraries are present), then compare both the path you get from your terminal as well as bash. Add the path you got from the terminal to your global environment in order to make sure the same python version is used everywhere.
You can also refer to this: Python modules not found over terminal but on python shell, Linux
I had the same issue. You might have multiple versions of python and you only installed scipy on the one you are not using
OR
you are using an IDE which has the option to use packages you install that are not by default in python. Pycharm has that. When you make a new project, it has a tick option saying "Inherit global site-packages" which means it will use additional packages you have installed any.

Installed python package with pip but can't import said package

I installed a python3 package called urllib3 using pip with the following command:
pip install urllib3
It seemed to be successful and when I type the command:
pip list
I get the following list which implies it is installed
But when I try to import in in the python console I get the following error:
ModuleNotFoundError: No module named 'urllib3
I am using Python 3.8.5 (64bit) with a VS Code virtual enviroment, I have tried uninstalling it and reinstalling I've also checked typos but I can't seem to find any.
try:
pip3 install urllib3
As it can be possible you have also installed python2 on your machine.
Also try restarting your IDE.
You're paths might be mixed up: python, python3 as well as pip and pip3 might not point to the places you're expecting.
Try to validate that which pip and which python point to the same python installation.
Alternatively, you can install urllib3 using python -m pip install urllib3 instead of pip install urllib3.
Lastly, it might be a VS Code issue. See if the information on selecting a Python interpreter are helpful to you.
The problem was with permissions for running scripts on the computer, I had to run the following command to enable permissions on my system:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
This enabled the current user (me) to actually activate the venv when I opened powershell (I think) and run the pip install inside the venv instead of outside.
Note that I had the package installed globally since the PowerShell was not inside the venv.
Thanks for the help
Resolved!

Flask app can't find google module

I'm using a virtual environment to run a flask app. When I run pip freeze, I get the following:
google-api-core==0.1.1
google-auth==1.2.1
google-cloud-core==0.28.0
google-cloud-speech==0.30.0
google-gax==0.15.16
googleapis-common-protos==1.5.3
However, during run time, I get the following error:
from google.cloud import speech
ModuleNotFoundError: No module named 'google'
I'm using the google speech APIs. They work just fine when I run them locally. I don't understand why the app can't find the modules even though they're listed as installed. Can someone suggest a fix? I've tried doing pip install google, and it downloaded a bunch of other stuff, but still no fix.
So there are a lot of places where the error could be coming from. Could you please provide more details?
For example, which python version are you using? Python 2 or 3? If you are calling the wrong interpreter you need to type
python3 -m pip install
or
python3 -m pip install
accordingly.
Secondly are you using conda? If so, you need to use
conda install
instead of pip install. You can find out by typing which python in your terminal.
Third, are you sure you installed the google module correctly? If not try using
pip install google --user
and see if that works.
Lastly, are you installing the correct package? Because I believe for the speech api you need to do:
pip install --upgrade google-api-python-client
Well, removing the virtual environment and reinstalling all the dependencies worked.
It might be easier to add to your flask.py the path to modules you already installed:
import sys
sys.path.append("/home/ubuntu/.local/lib/python3.6/site-packages/")
import google
import gspread
That worked like magic to me at AWS.

"ImportError: no module named 'requests'" after installing with pip

I am getting ImportError : no module named 'requests'.
But I have installed the requests package using the command pip install requests.
On running the command pip freeze in the command prompt, the result is
requests==2.7.0
So why is this sort of error happening while running the python file?
Run in command prompt.
pip list
Check what version you have installed on your system if you have an old version.
Try to uninstall the package...
pip uninstall requests
Try after to install it:
pip install requests
You can also test if pip does not do the job.
easy_install requests
I had this error before when I was executing a python3 script, after this:
sudo pip3 install requests
the problem solved, If you are using python3, give a shot.
One possible reason is that you have multiple python executables in your environment, for example 2.6.x, 2.7.x or virtaulenv. You might install the package into one of them and run your script with another.
Type python in the prompt, and press the tab key to see what versions of Python in your environment.
In Windows it worked for me only after trying the following:
1. Open cmd inside the folder where "requests" is unpacked. (CTRL+SHIFT+right mouse click, choose the appropriate popup menu item)
2. (Here is the path to your pip3.exe)\pip3.exe install requests
Done
if it works when you do :
python
>>> import requests
then it might be a mismatch between a previous version of python on your computer and the one you are trying to use
in that case : check the location of your working python:
which python
And get sure it is matching the first line in your python code
#!<path_from_which_python_command>
Opening CMD in the location of the already installed request folder and running "pip install requests" worked for me. I am using two different versions of Python.
I think this works because requests is now installed outside my virtual environment. Haven't checked but just thought I'd write this in, in case anyone else is going crazy searching on Google.

Categories