No module named 'requests' Python 3.5.0 - python-3.x

I need to use requests in my code but it says that it's not installed. I get the following error: No module named 'requests'. It's actually installed and works in python 2.7: Requirement already satisfied (use --upgrade to upgrade): requests in /Library/Python/2.7/site-packages. I searched a lot and tried to reinstall it, to download missing libraries, and etc... but nothing helped. How can I make it work on python 3.5 ?

Python 2 and 3 (and their packages) can be installed and co-exist independently of one another. I would suggest using pip as the best way to install Python packages (and keep them up-to-date).
Install pip for Python 3 – using one of the following methods:
easy_install (may be available as easy_install-3.5),
the distribution package manager (if running GNU/Linux) or
python -m ensurepip (thanks, Mark Dickinson)
Use pip to install the requests module:
pip3 install requests

You can even place the "requests" Folder (https://github.com/kennethreitz/requests/tree/master/requests) next to your "script.py" and import requests from any script within the folder containing the requests folder > root
--> requests
----> init.py
----> [more]
--> script.py
script.py can now import requests as always

The fastest and shortest answer would just be to do
pip install requests
And if you encounter a error try doing
sudo pip install requests
More info on this module can be found here
https://pypi.org/project/requests/

Related

Competing Python versions

On my Mac, I have 2 versions of Python running, one from Brew (3.9) and another one (3.8)
When I try to install some data science packages via pip3, they are installed but cannot be used as 3.9 takes over.
python3 --version
Python 3.9.10
which python3
/opt/homebrew/bin/python3
pip3 install pandas
Requirement already satisfied: pandas in /Library/Python/3.8/site-packages
python3
import pandas
ModuleNotFoundError: No module named 'pandas'
pandas is just one example of the many packages I see via pip3 freeze.
What are my options to point to installed versions at Python 3.8 ?
There are different ways to approach this:
One temporary option is to use alias like this:
alias python3=python3.8
You can use directly python3.8 in your terminal.
If you want to install the packages to python3.9 then you can also use pip3.9
You could use:
python3 -m pip install module
which will use pip to install to the python version you just used.
The recommended way is to use:
python3 -m pip install module

Dockerfile error with "RUN pip install -r requirements.txt" [duplicate]

I am working on developing a python package. I use pip freeze > requirements.txt to add the required package into the requirement.txt file. However, I realized that some of the packages, instead of the package version, have some path in front of them.
numpy==1.19.0
packaging==20.4
pandas # file:///opt/concourse/worker/volumes/live/38d1301c-8fa9-4d2f-662e-34dddf33b183/volume/pandas_1592841668171/work
pandocfilters==1.4.2
Whereas, inside the environment, I get:
>>> pandas.__version__
'1.0.5'
Do you have any idea how to address this problem?
It looks like this is an open issue with pip freeze in version 20.1, the current workaround is to use:
pip list --format=freeze > requirements.txt
In a nutshell, this is caused by changing the behavior of pip freeze to include direct references for distributions installed from direct URL references.
You can read more about the issue on GitHub:
pip freeze does not show version for in-place installs
Output of "pip freeze" and "pip list --format=freeze" differ for packages installed via Direct URLs
Better freeze of distributions installed from direct URL references

Can't import 3rd party python module

I would like to preface with the fact that I am a coding newbie and all this is very new to me. In fact, this is my first stack overflow question.
Anyways I am learning python and am trying to install my first third party module called “pyperclip” in Terminal, but I don't think it installed correctly. I am very new with Terminal as well.
For reference, I am following the youtube guide "Automate the Boring Stuff with Python"
https://www.youtube.com/watch?v=xJLj6fWfw6k
I am on Lesson 8 and I am at minute 3:43 in the video if you want to follow along.
Here are some of my system details:
Mac OS X
Python version 3.8.3
I ran the following into Terminal:
sudo pip3 install pyperclip
Then I received the following message in Terminal after installing pyperclip
The directory or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
I then tried to install again using the following:
sudo -H pip3 install pyperclip
Then I got this message:
Requirement already satisfied: pyperclip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.8.0)
I tried installing one last time using:
pip3 install pyperclip
I got the same message again:
Requirement already satisfied: pyperclip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.8.0)
This is also the error I get when I try importing pyperclip in my interactive shell:
>>> import pyperclip
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ModuleNotFoundError: No module named ‘pyperclip’
>>>
Can anyone help me please? I feel like a fish out of the water with all this stuff. Especially when it comes to Terminal. Any help would be greatly appreciated.
I wanted to discuss a few things that might help you on your way.
We typically do not use sudo in conjunction with pip to install Python packages. This installs the package at a system level and could conflict with packages that your system currently has installed or may install packages to a Python installation which is different than the one you are using from the terminal. Instead, we typically use pip install --user rather than sudo pip install.
See this question and answer for more: sudo pip install VS pip install --user.
If you are ever unsure whether a package has been installed properly, check using
pip3 list
We also need to make sure that the Python interpreter you are using is the same as where pip is installing. Since you are using pip3 to install, you should be using python3 at the terminal to enter the interactive shell. You can also verify that you are using the right Python interpreter by typing in the following command into the terminal:
which python3
Then, make sure that the output matches with the /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ path that was reported by pip3.
If you are using a different interactive shell such as ipython, you need to make sure that you install ipython in the same manner using
pip3 install --user ipython
and executing it using
ipython3
Try repeating your steps using this new information to see if it helps. Let me know if you have any more questions or need some more help.

ModuleNotFoundError: No module named 'requests'. But 'requests' already installed

Trying to import 'requests'.
Has it installed via pip3 install requests? But still, have this error.
C:\Users\Vikentiy>pip3 list
Package Version
---------- ----------
certifi 2018.11.29
chardet 3.0.4
Django 2.1.7
idna 2.8
pip 19.0.2
pytz 2018.9
requests 2.21.0
setuptools 40.6.2
simplejson 3.16.0
urllib3 1.24.1
virtualenv 16.4.0
C:\Users\Vikentiy>python --version
Python 3.7.2
Error Traceback:
C:\Users\Vikentiy\untitled2\venv\Scripts\python.exe C:/Users/Vikentiy/untitled2/requeststests.py
Traceback (most recent call last):
File "C:/Users/Vikentiy/untitled2/requeststests.py", line 1, in <module> import requests`
Try to uninstall requests and then again install it using pip
pip uninstall requests
and again install it
pip install requests
or if you didn't get what I am saying so please visit
https://scmquest.com/resolved-importerror-no-module-named-requests-usr-bin-python-no-module-named-pip-on-macos/
Another cause of this might be multiple Python versions installations. So if you type in your terminal:
pip3 install requests
The requests library will be automatically installed to your default python3, for example, Python3.6. However, let's say you have another Python version, for example, Python3.7, what you should do is:
pip3.7 install requests
This should solve this problem.
If you are running your code in the terminal, you should try:
python3.7 file_to_run.py
If you're using PyCharm as IDE, try closing and restarting PyCharm. Move the mouse cursor over the "requests" (with red curly line beneath it), until the red light bulb next to it show up. Select the first option in it, "Install package requests". PyCharm will take care of the installation from there.
I ran into the same issue and have tried all the solutions here on Stack Overflow.
If it is installed this will WORK:
c:\>python (or on Mac/Linux "$ python")
>>> import requests
>>> requests.get("http://127.0.0.1")
<Response [200]>
If you see this error when running your script/IDE:
Traceback (most recent call last): File "E:\test.py", line 1, in <module>
import requests
ImportError: No module named requests
Try:
python script.py
Answer is based on https://www.edureka.co/community/84584/python-requests-module-import-error-module-named-requests
Then the trick for me was not to start up a VirtualEnv, I actually found I was running the x64 version of python when I'd installed the requests package into the python 32 bit folder c:\program files (x86)\python37-32\lib\site-packages. This is a screenshot from the internet but it shows you how to change the interpreter you're using - in my case - I need to set it to Python 3.7.4(x86) 32 bit:
Run in command prompt
and write command pip install requests in scripts directory
cd \Python27\scripts
pip install requests
That's the exact solution which works for me, please follow this:
As like, first try to get the version of the requests module installed previously, this can be find by
pip install requests
Now, you will get the message:
Requirement already satisfied: requests in /opt/anaconda3/lib/python3.8/site-packages (2.24.0)
See, here the version of the requests module i.e., (2.24.0)
Now, the simple basic logic is this like you should install just the previous version of the requests (2.24.0). So, you should now install requests (2.20.0)
For this use command:
pip install requests==2.20.0
Now, I think you can test import requests and this will work fine.
Further, if any error occurs then please let me know in the comments.
Thanks,
HaPpY Coding 🤗
This issue is due to missing request module in python package .So you can use below command to install and recheck by restarting your IDE
pip install requests
If already installed better to upgrade
pip install --upgrade pip
If you are using venv (virtual environment), you may need to consider where it's path. for my case it was outside my project folder. So I had to first install request inside the main python's path then I recreate venv inside my project.
inside_your_project#python -m venv ./venv

python install wheel leads to import error

I'd like to make a wheel binary distribution, intstall it and then import it in python. My steps are
I first create the wheel: python ./my_package/setup.py bdist_wheel
I install the wheel: pip install ./dist/*.whl
I try to import the package: python -c"import my_package"
This leads to the error:
ImportError: No module named 'my_package'
Also, when I do pip list, the my_package is listed.
However, when I run which my_packge, nothing is shown.
When I run pip install ./my_package/ everything works as expected.
How would I correctly build and install a wheel?
python version 3.5
pip version 10.1
wheel version 0.31.1
UPDATE:
When I look at the files inside my_package-1.0.0.dist-info, there is an unexpected entry in top_level.txt. It is the name of the folder where I ran
python ./my_package/setup.py bdist_wheel in. I believe my setup.py is broken.
UPDATE WITH REGARDS TO ACCEPTED ANSWER:
I accepted the answer below. Yet, I think it is better to simply cd into the package directory. Changing to a different directory as suggested below leads to unexpected behavior when using the -d flag, i.e. the target directory where to save the wheel. This would be relative to the directory specified in the setup.py file.
I had the very same error, but it was due to my setup.py not specifying the entry "packages=setuptools.find_packages()".
Everythings builds nicely without that but you can't import anything even though pip shows it to be installed.
If you need to execute the setup script from another directory, ensure you are entering the project dir in the script.
from setuptools import setup
root = os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))
os.chdir(root)
# or using pathlib (Python>=3.4):
import patlib
root = pathlib.Path(__file__).parent
os.chdir(str(root))
setup(...)
In my case, in order to solve it I just had to upgrade pip (since Docker installed pip 9).
python3 -m pip install --upgrade pip
I have experienced the same situation, maybe not for the same reason, here just for reference.
The package name should not contain the dash "-", there's no error pop out, but after installing your wheel, though it is shown in pip list, you can't find that package.
/src/your-package-name # should not
/src/your_package_name # should like this
In the setup.py, you can use the name with dash "-" without limitation:
setuptools.setup(
name="instrument-lab",
...

Resources