Python request module not working - python

I installed Python using Homebrew, I installed Pip and then using Pip I downloaded the Requests module.
Just to double check I tried to install it again and got a confirmation in terminal that it was already installed:
Requirement already satisfied: requests in /usr/local/lib/python2.7/site-packages
I then tried to write a pretty simple script to call Git and see if I can get a response. But when I run it I get an error message. Here's the script and message:
import requests
response = requests.get('https://api.github.com/events')
print (response)
Message:
PythonTesting/main.py", line 5, in <module>
import requests
ImportError: No module named requests

I'm a beginner in programming. Today, I was in the same problem. I spent many hours trying fix this problem. For me worked:
open prompt comand
cd (path of scripts directory in venv - my project on VisualStudio Code)
pip install requests
Then, I opened VS and put my code to go. It's work well.

Make sure you are running the code in version 2.7 as you have installed the request package for 2.7 only. If by mistake you are running the code on version 3, then that request package won't work. In that case, you need to install the requests for python3.

Related

Having issue importing requests and I assume it's my python runtime not being set correctly

Probably stupidly I tried to install the latest version of Python, in this case using the download from python site, but after doing that I was then getting python still running on the previous version python-3.6. I'm on OSX and was using sublime.
So I have been trying to work out how to update it to use the newest version. I've followed; https://opensource.com/article/19/5/python-3-default-mac.
All of the responses to queries now point to the python-3.9.5 version. So that's great and my runtime is using that. However after installing the requests using pip install I get the following error when running.
import requests
ModuleNotFoundError: No module named 'requests'''
[path: /Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
I stumbled upon Modules are installed using pip on OSX but not found when importing which I have been trying to work through.
I have been able to run the import command successfully in terminal, however it's intermittent as I've tried again and it's broken, so I'm lost. I'm running it something trying to run the python3.6 version, which after updating I followed these instructions to remove when I have uninstalled that from my mac https://www.macupdate.com/app/mac/5880/python/uninstall.
If there is any ideas, would love some help, mainly to try and tell me what that error message is telling me.
In particular, what does this mean?
[path: /Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.6/
I should clarify too; when I run 'pip list'
I see
requests 2.25.1
Assuming that you are not installing requests package properly, and assuming your python executable is named python:
python -m pip install requests
If however, your python executable is named something else instead, e.g. python3, replace python with that name:
python3 -m pip install requests

'ModuleNotFoundError' on local editor but not in Jupyter

I'm trying to use Python's requests module, so I installed it using
pip install requests,
then it appears that requirement is already satisfied in Miniconda library. On my script, I import request as
import requests,
and when I run the script I get ModuleNotFoundError. However, if I do the same thing in Jupyter, the module works and no error appears. What should I do to use that module locally in my editor?

python cannot find module that is installed on windows

I have installed the requests module
C:\Python34\Scripts\easy_install.exe requests
i got to the folder location
C:\Python34\Lib\site-packages\requests-2.13.0-py3.4.egg\requests
I have a path variable in system
C:\Python34\Lib\site-packages
yet when i run my script
C:\Users\beast\Desktop>update.py
I get the error No module named 'requests'
Traceback (most recent call last):
File "C:\Users\beast\Desktop\plex_playlist_update.py", line 17, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
I tried installing using pip just in case
python -m pip install requests
Requirement already satisfied: requests in c:\python34\lib\site-packages\requests-2.13.0-py3.4.egg
I am new to python and I cant find an answer anywhere.
UPDATE:
I found a command to check my python search location.
C:\Users\beast\Desktop>python -c "import site; print(site.getsitepackages())"
['C:\\Python34', 'C:\\Python34\\lib\\site-packages']
I think it has to do with C:\Python34 vs C:\python34? How do i check or fix this?
The problem here is maybe because of the different versions of python installed.
If you are a Windows user, you can go to Path in Environment variables and remove the paths to unnecessary versions of python (if any). Modules installed for one version of python won't work in another version.
So I ended up going with python environment. Per python documentation it is the way to go anyway. The below command are run on the root folder of the python app.
py -m venv env
then
./env/Scripts/activate
I then ran my pip upgrade and everything is working. Obviously this did not fix it computer wide. just for my python app. But from documentation this is better because of version control of the whole enviroment.
This creates a "env" folder at the root which will have all the packages installed. Need to install all requirements again or use the requirements.txt file.

BeautifulSoup4 is not working even though I have successfully installed it using pip Linux Mint Sarah

I am a brand new linux user using linux mint sarah and I just installed python 3.5 as well as BeautifulSoup to do some web scraping.
However, when I type in the following command I receive a traceback error:
from bs4 import BeautifulSoup
The error tells me that there is no module bs4.
I have also tried:
import bs4
from BeautifulSoup import BeautifulSoup
import beautifulsoup
This is weird because if I go into terminal and give the command pip list, it shows me a list of all my programs and it states that I have beautifulsoup4 (4.5.1)
I successfully used pip in the same exact way to install a module called requests and it was successful.
One thing that I think may be getting in the way is that Linux mint comes with python 2.7 installed and my modules are going into a 2.7 folder which seems odd. (However, the requests module that I can successfully use is in the same folder as the BeautifulSoup4 module)
I must admit I have not tried easy_install because it gives me some error about the directory not existing when I try to install BeautifulSoup4 that way.
I'm muddying the waters too much so I will leave it at that. Hopefully, somebody can help me figure out whats going on so that people who have this problem in the future can benefit.
Thanks!
You need to install BeautifulSoup4 for Python 3.5.
Option 1:
Download https://bootstrap.pypa.io/get-pip.py file to server.
Run python3 get-pip.py
Run pip3 install beautifulsoup4
Option 2:
Download https://pypi.python.org/pypi/beautifulsoup4 to server.
Extract tar.gz: tar -xvzf beautifulsoup4-4.5.1.tar.gz
Go to folder: cd beautifulsoup4-4.5.1/
Install from source: python3 setup.py install

Python mechanize module not found

My python version is: Python 2.7
I did an easy_install mechanize then I tried running a script and I'm getting this:
File "test.py", line 2, in <module>
import mechanize
ImportError: No module named mechanize
If you need any other information let me know.
Try this in case pip or easy_install, for whatever reason, aren't working:
Download the source code from http://pypi.python.org/packages/source/m/mechanize/mechanize-0.2.5.tar.gz
Unzip the package, go to the command line, change into the folder that was extracted and type:
python setup.py install
If you're on Linux/Mac, you may need to type the following:
sudo python setup.py install
Would like to add something for some future users:
In case some of the users get the error "No module named '_version'"
Mechanize however doesn't support Python 3 so you should install a Python 2.x release (the latest available is 2.7.3), install the mechanize package there and then retry running your script.

Categories