'No module named requests' even if I installed requests with pip - python

I'm trying to test if requests module has been well installed. But I'm getting the following error :
raceback (most recent call last):
File "/Users/macbookpro/Desktop/test.py", line 1, in <module>
import requests
ImportError: No module named requests
when trying to run the following test script:
import requests
print 'test'
But I have installed requests with pip, and pip list command gives the following result :
MBPdeMacBook2:~ macbookpro$ pip list
arrow (0.7.0)
beautifulsoup4 (4.4.1)
classifier (1.6.5)
coursera-dl (0.6.1)
Django (1.8.6)
html5lib (1.0b8)
keyring (9.0)
lxml (3.6.0)
Pillow (3.4.2)
pip (8.0.2)
pyasn1 (0.1.9)
requests (2.14.2)
setuptools (19.4)
six (1.10.0)
urllib3 (1.16)
vboxapi (1.0)
virtualenv (13.1.2)
wheel (0.26.0)
Why requests isn't being imported ?
EDIT :
MBPdeMacBook2:~ macbookpro$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
MBPdeMacBook2:~ macbookpro$ which pip
/usr/local/bin/pip
MBPdeMacBook2:~ macbookpro$ python --version
Python 2.7.11
MBPdeMacBook2:~ macbookpro$ pip --version
pip 8.0.2 from /usr/local/lib/python2.7/site-packages (python 2.7)

In general, you should get into the habit of working in a virtualenv. I find the documentation here to be helpful.
If you install all of your dependencies within the virtual environment, you'll be (mostly) sure that you are installing those deps. in the same environment that you're running the jobs in.
For your case, on the command line go to the directory where your code lives and run
pip install virtualenv
virtualenv my_project
source my_project/bin/activate
Now that the virtualenv is active you can
pip install requests
Only what is installed in the virtualenv will be available. This will keep your system clean. Each project should get its own virtualenv, meaning only the dependencies needed for each project will be available to them. This way you could, say, have version 1 of some dependency installed for one project and version 2 for another. They won't come into conflict.
After you have installed all the dependencies, run
pip freeze > requirements.txt
To get a list of all the dependencies for the project saved. Next time you need to install these, you simply run
pip install -r requirements.txt
Once you are done working in the virtualenv, run
deactivate

I am not 100% sure, but the paths from which python and which pip may indicate that you have two versions installed. The Python version being the old one that was shipped with OS X, and another version.
I would advice you to install Python27 (or even better Python3) from brew.
You can install brew with a single command, and another one for installing Python27/3. When this is done you set the PATH variable in your shell rc file and you should be good to go.
I have Python27 installed (via brew) and my (working environment) reports the following paths:
which python: /usr/local/bin/python
which pip: /usr/local/bin/pip
And
python --version: 2.7.15
pip --version: pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python2.7)

I experienced this same issue on Ubuntu 18.04 LTS, to check that, I first checked if requests library was installed on my system or not.
Run these commands on your terminal inside the virtual environment in which you working
$ python
Then the python command line opens, then run
>>> import requests
After this if you get an ImportError saying, No module named requests, then it means the dependency has not been installed properly. If there is no such error, then it means the dependency is installed successfully.

This can occur for example if pip is actually pip3 and python is actually python2.7.
In your case the which pip and which python eliminate this possibility but it just happened to me.
The solution was to do pip2 instead of pip; if the situation was reversed you can use pip3.

Simply go inside your virtual environment and run below commands:
1). pip install --user pipenv
2). pipenv install requests
after executing the above commands cd to your app folder inside the virtual environment and just run it. Hopefully now it will run.
Reference link: https://python-guide-pt-br.readthedocs.io/pt_BR/latest/dev/virtualenvs.html#make-sure-you-ve-got-python-pip

Related

Why isn't Pip working inside Python 3.6.9 Virtual Environment?

Specs:
I am running a Ubuntu Server 20.04.1 LTS Virtual Machine in HyperV (with a GUI installed).
I run this, and see it came with Python 3.8.5:
$ python3 --version
Python 3.8.5
Goal:
I need to:
Install Python 3.6.9 in my VM.
Go to an existing repository directory, and create a Virtual Environment for Python 3.6.9.
Install the following packages in the Python 3.6.9 Virtual Environment:
Implementation:
First, I downloaded the Python 3.6.9 source files from https://www.python.org/downloads/release/python-369/ and extracted them to a directory in my VM.
Then, I moved to said directory, and ran these commands:
$ time ./configure
$ time make
$ time sudo make install
$ time make clean
Then, I ran these commands, and saw that Python 3.6.9 is now installed.
$ python3 --version
Python 3.8.5
$ python3.6 --version
Python 3.6.9
Then I installed pip and it's package virtualenv. If I run these commands I get:
$ pip --version
pip 21.0.1 from /home/ubuntu/.local/lib/python3.8/site-packages/pip (python 3.8)
$ virtualenv --version
virtualenv 20.4.0 from /usr/local/lib/python3.8/dist-packages/virtualenv/__init__.py
I will test this on a new directory in my Desktop. Once I'm sure it works, I'll do it in the repository where I need it.
I cd to my Desktop. Then I run this, to see where Python 3.6.9 is installed in my VM:
$ which python3.6
/usr/local/bin/python3.6
Then I run:
$ virtualenv -p /usr/local/bin/python3.6 testenv3.6
A new directory testenv3.6 is successfully created in my Desktop. I cd to it and run:
$ source ./bin/activate
(testenv3.6) $ python3 --version
Python 3.6.9
Everything seems fine.
Then I tried to run pip install pandas, and got this Error:
from pip._vendor import html5lib, requests
ImportError: cannot import name requests
This also happened when I tried to install numpy or any other package. Seems similar to this: ImportError: cannot import name requests
So I went to the file causing the Error. I can't remember exactly where it was, but I know I changed it from this:
from pip._vendor import html5lib, requests
to this:
from pip._vendor import html5lib
import requests
Then I try to run pip install pandas or pip install numpy again, and I get:
ModuleNotFoundError: No module named 'zlib'
Once again I searched on Stack Overflow, and found this: no module named zlib
According to that URL's answer, I tried to run sudo apt-get install zlib1g-dev. I get this:
zlib1g-dev is already the newest version (1:1.2.11.dfsg-2ubuntu1.2).
The following packages were automatically installed and are no longer required:
libpython3.6-minimal libpython3.6-stdlib python3.6-minimal
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 104 not upgraded.
If I try to run pip install pandas or pip install numpy again, the same error remains:
ModuleNotFoundError: No module named 'zlib'
I also tried pip install zlib, but it doesn't work.
I searched multiple Stack Overflow questions for similar issues. People said to:
Downgrade and install a specific version of pip.
Downgrade and install a specific version of virtualenv.
Try to install the desired Python version from other sources.
Try to install Pip from other sources.
Nothing seems to work.
I also noticed that the pre-installed Python versions were in /bin/, while my installation of python 3.6.9 was in /usr/local/bin/.
So I even tried moving the Python 3.6.9 file next to the other pre-installed Python versions.
So before, I got this:
$ which python3.6
/usr/local/bin/python3.6
And now, I get this:
$ which python3.6
/bin/python3.6
It still didn't fix it.
Now, after all my attempts, I might have installed Python in multiple places, and turned my system into a mess. I'm not sure.
Help?

Error : No module named 'psutil'

I've been trying to install Peter Chervenski's MultiNEAT and ran into a problem while running setup.py (python setup.py build_ext):
File "c:/Users/1234/Documents/Alex/multineat/peter-ch-MultiNEAT-f631e2f/setup.py", line 7, in
from site-packages import psutil'
And I made sure this module is installed: used pip install a couple of times and it said:
Requirement already satisfied: psutil in
c:\users\1234\appdata\local\programs\python\python36-32\lib\site-packages
And I checked this directory myself and found psutil module there (I even deleted it and reinstalled one more time). And after that, I still got the same error with Python not seeing psutil. Is there any way I can solve this problem? (I am using Windows 10, latest version of Python)
Error I got in windows 10 while executing a python script ->
Traceback (most recent call last):
File "C:\Users\YOUR NAME\Desktop\Sample.py", line 2, in
import psutil
ImportError: No module named psutil
This is how I resolved the issue ->
C:\python -m pip install --upgrade pip
[Latest pip version got installed successfully]
C:\>python -m pip install psutil
Collecting psutil
Downloading https://files.pythonhosted.org/packages/50/6a/34525bc4e6e153bf6e849a4c4e936742b365f6819c0462cebfa4f082a3c4/psutil-5.4.7-cp27-none-win_amd64.whl (220kB)
100% |UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU| 225kB 696kB/s
Installing collected packages: psutil
Successfully installed psutil-5.4.7
Finally, Sample.py got executed successfully
This worked for me..
sudo pip install --upgrade psutil
Follow these steps on windows:
Open command prompt in administrator mode
Enter Command python -m pip install psutil
Upgrade pip if outdated python -m pip install --upgrade pip
Process Example
I solved a problem and if you had a similar problem you should try this:
upgrade pip to the latest version (python -m pip install --upgrade pip)
delete psutil (C:\Users\(your username)\AppData\Local\Programs\Python\Python36-32\Lib\site-packages) if
you have it installed already
reupload psutilnow using pip install psutil
. It helped me and if you have a permission error, try opening cmd as administrator.
I had that problem trust me this works 100%. I am using VS Code you just have to install it with pip normally but sometimes it doesn't add it to your working directories (venv\lib\site-packages\) folder your editor is using. So just copy and paste it there from here C:\Users\(your username)\AppData\Roaming\Python\Python39\site-packages
I resolved this issue as below:
It seems, every time i was trying to upgrade psutil using "pip install psutil" it just showing the requirement is satisfied and showing 5.7.2.
Go to the folder where Python is install and psutil is available e.g.
"C:\Python\Lib\site-packages"
find all psutil related filesand rename them to something else e.g.
Old__psutil
Old_psutil-5.7.2.dist-info
Run "pip install psutil" from bash or command line
It updated for me to 5.8.0
This resolved my issue.
Inconsistencies can happen if you have multiple versions of windows installed on your system. Check for the default python directory path and make sure 'psutils' is updated in that directory.
I got the same issue and solved it by specifying the exact (virtual environment / global) pip you are using. In my case (using a virtual environment), after executed pip by using the absolute path, I installed psutil successfully.
I was having same issue, but not quite exactly the same circumstances. I am writing this for any people that have the same problem as I did.
I was running sudo python script.py (sudo was needed since I wanted to alter cpu frequencies) but Error : No module named 'psutil' appeared. I checked my python version with
python --version
and it was 3.8, but when I checked my version with
sudo python --version
it was running version 2.7. Once I ran
sudo python3 script.py
it ran smoothly. Of course I had already verified I had the newest pip installed + psutil library installed on python3. Basically, make sure you are running the correct python version and you have all libraries/package managers up to date, hope this helped!
This worked for me. I had multiple version of python in my ubuntu system.
$ sudo apt install python3.6-dev
These two commands typed in the SSH shell worked for me. The apt install is what was showing me the error mentioned.
sudo apt install pip
python3 -m pip install psutil
Result:
python3 -m pip install psutil
Collecting psutil
Downloading psutil-5.9.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (284 kB)
|████████████████████████████████| 284 kB 2.0 MB/s
Installing collected packages: psutil
Successfully installed psutil-5.9.1
Hope this helps someone...

pip with default python version OSX

I'm trying to write a script that uses speedtest-cli, I have installed the module with pip install speedtest-cli. If I run pip install speedtest-cli I get the following
Requirement already satisfied: speedtest-cli in /usr/local/lib/python2.7/site-packages
If I try to use pip with the default python, as recommended by another answer here, like so python -m pip install speedtest-cli I get
/usr/bin/python: No module named pip
I figured that the pip is setup for one of the other versions of python but the only version of python in /usr/local/bin is python3 and in /usr/bin there is python, python2.6, and python2.7 but I have tried defining each ones of those as the interpreter at the top of the file and none of them works, each producing giving the same import error on the speedtest module.
Which version of python is pip linked to then, or why is the installed module not importing?
The easiest solution was to completed a fresh start.
Remove all references to easy_install, python, and pip using the following;
rm -r easy_install*
rm -r pip*
rm -r python*
Then I used this guide to properly install, setup, and configure python with HomeBrew, with pip and virtualenv installed.
The article is fairly lengthy so I'm not going to copy it all out here but here is an image snapshot in case the link goes dead.

No module named google.protobuf

I am trying to run Google's deep dream. For some odd reason I keep getting
ImportError: No module named google.protobuf
after trying to import protobuf. I have installed protobuf using sudo install protobuf. I am running python 2.7 OSX Yosemite 10.10.3.
I think it may be a deployment location issue but i cant find anything on the web about it. Currently deploying to /usr/local/lib/python2.7/site-packages.
There is another possibility, if you are running a python 2.7.11 or other similar versions,
sudo pip install protobuf
is ok.
But if you are in a anaconda environment, you should use
conda install protobuf
Locating the google directory in the site-packages directory (for the proper latter directory, of course) and manually creating an (empty) __init__.py resolved this issue for me.
(Note that within this directory is the protobuf directory but my installation of Python 2.7 did not accept the new-style packages so the __init__.py was required, even if empty, to identify the folder as a package folder.)
...In case this helps anyone in the future.
In my case I
downloaded the source code, compiled and installed:
$ ./configure
$ make
$ make check
$ sudo make install`
for python I located its folder(python) under source code, and ran commands:
$ python setup.py build
$ python setup.py install'
Not sure if this could help you..
I got the same error message when I tried to use Tensor Flow. The solution was simply to uninstall Tensor Flow and protobuf:
$ sudo pip uninstall protobuf
$ sudo pip uninstall tensorflow
And reinstall it again: pip installation of Tensorflow. Currently, this is:
# Ubuntu/Linux 64-bit, CPU only:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Mac OS X, CPU only:
$ sudo easy_install --upgrade six
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py2-none-any.whl
when I command pip install protobuf, I get the error:
Cannot uninstall 'six'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
If you have the same problem as me, you should do the following commands.
pip install --ignore-installed six
sudo pip install protobuf
According to your comments, you have multiply versions of python
what could happend is that you install the package with pip of anthor python
pip is actually link to script that donwload and install your package.
two possible solutions:
go to $(PYTHONPATH)/Scripts and run pip from that folder that way you insure
you use the correct pip
create alias to pip which points to $(PYTHONPATH)/Scripts/pip and then run pip install
how will you know it worked?
Simple if the new pip is used the package will be install successfully, otherwise the package is already installed
I installed the protobuf with this command:
conda install -c anaconda protobuf=2.6.1
(you should check the version of protobuf)
In my case, MacOS has the permission control.
sudo -H pip3 install protobuf
I had this problem to when I had a google.py file in my project files.
It is quite easy to reproduce.
main.py: import tensorflow as tf
google.py: print("Protobuf error due to google.py")
Not sure if this is a bug and where to report it.

Installing python packages with multiple versions on OSX

I am attempting to install a package for python3.4 on Mac OSX 10.9.4. As you know, python ships with OSX, so when I installed python3.4 I was happy to find that it came with its own version of pip, that would install packages to it (installing pip on a mac with multiple versions of python will cause it to install on the system's python2.7.)
I had previously tried installing this package (https://pypi.python.org/pypi/chrome/0.0.1) with my first installation of pip (the one tied to python2.7) and found that it successfully installed on that version, but not on any others.
I ran an install with the new pip keyword for python3.4 (which when called by itself spits out the help page so i know it works) and it told me that the package was already installed and to try updating. The update revealed that I already had the most recent version. so I tried uninstalling it from just the python3.4 and reinstalling to no avail, and got the same results when uninstalling pip from python2.7 and reinstalling only on version 3.4.
I know that's a bit hard to follow but hopefully that makes sense.
I also reviewed the content here with no success.
RESOLVED:
while python did have a directory named the same as a directory it uses with packages, this was not the correct directory, for me it was in a subdirectory of library. while documentation said that referencing pip2 would cause the package to install on python3.4, this was false. however, referencing pip3.4 worked for me.
My suggestion is that you start using virtualenv.
Assuming you have 3.4 installed, then you should also have pyvenv. As for pip and 3.4, it should already be installed.
Using for example version 3.4 create your own virtual environment and activate it:
$ mkdir ~/venv
$ pyvenv-3.4 ~/venv/py34
$ source ~/venv/py34/bin/activate
$ deactive # does what is says...
$ source ~/venv/py34/bin/activate
$ pip install ... # whatever package you need
With version 2.7 first install virtualenv and then create your own virtual environment and activate it. Make sure that setuptools and pip are updated:
$ virtualenv-2.7 ~/venv/venv27
$ . ~/venv/venv27/bin/activate
$ pip install -U setuptools
$ pip install -U pip
$ pip install ... # whatever package you need

Categories