Importing Requests not Working Python 3.7 - python

I'm running the latest Kali Linux with Python 3.7 and am trying to use the requests module. As you can see, I install requests but still cannot import it. Could someone help me reconcile this strange problem?
root#kali:~/Desktop# pip3 install --upgrade requests
Requirement already up-to-date: requests in
/usr/local/lib/python3.7/site-packages (2.20.1)
Requirement already satisfied, skipping upgrade: urllib3<1.25,>=1.21.1
in /usr/local/lib/python3.7/site-packages (from requests) (1.23)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests) (3.0.4)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests) (2018.10.15)
Requirement already satisfied, skipping upgrade: idna<2.8,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests) (2.7)
root#kali:~/Desktop# python3
Python 3.5.6 (default, Sep 29 2018, 21:41:04)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'
Thanks ahead of time for the help!

Since it appears you have multiple versions of Python installed you'll need to set your environment to use Python 3.7 (currently it's set to 3.5.6, and requests doesn't appear to be installed).
In your .bash_profile you could probably do:
alias python3=/path/to/python3.7
which should use the 3.7 version when you enter python3 ...

Related

Module found on python shell / interactive prompt, but getting ModuleNotFound on import in file

I am trying to use python-twitter (https://python-twitter.readthedocs.io/en/latest/index.html), and I am not having much luck. The python-twitter module is invoked with import twitter, unfortunately when I attempt to do that via my script from the command line it isn't working.
What's boggling my mind, is that there is no similar error when executing from the python shell.
Everything is installed correctly, it would seem:
(venv) user#computer:~/Documents/projectdir$ pip3 install python-twitter
Requirement already satisfied: python-twitter in /home/user/.local/lib/python3.8/site-packages (3.5)
Requirement already satisfied: requests-oauthlib in /home/user/.local/lib/python3.8/site-packages (from python-twitter) (1.3.0)
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from python-twitter) (2.22.0)
Requirement already satisfied: future in /usr/lib/python3/dist-packages (from python-twitter) (0.18.2)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/lib/python3/dist-packages (from requests-oauthlib->python-twitter) (3.1.0)
(venv) user#computer:~/Documents/projectdir$ pip3 install twitter
Requirement already satisfied: twitter in /home/user/.local/lib/python3.8/site-packages (1.18.0)
(venv) user#computer:~/Documents/projectdir$ pip3 freeze > requirements.txt
(venv) user#computer:~/Documents/projectdir$ echo "$(<requirements.txt)"
...
python-twitter==3.5
...
twitter==1.18.0
...
And yet my script throws an error:
(venv) user#computer:~/Documents/projectdir$ sudo python3 authentication_test.py
Traceback (most recent call last):
File "authentication_test.py", line 1, in <module>
import twitter
ModuleNotFoundError: No module named 'twitter'
Despite the twitter module being found by the python shell:
(venv) user#computer:~/Documents/projectdir$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
>>> twitter.Api()
<twitter.api.Api object at 0x7f11b0056a60>
>>>
What's going on here? I thought maybe there was something causing issues between twitter and python-twitter, but I did pip3 uninstall twitter and the issue persists.
I should note that although I broke them up with comments, the above code snippets were done consecutively.
Ideas?
As stated by Aaron (thank you), the answer was that sudo was taking me out of the virtualenv context, and thus the packages were not available.

ImportError: No module named request. The error isn't coming from my code

I'm trying to import a package called populartimes: https://github.com/m-wrzr/populartimes
I keep getting this result:
Traceback (most recent call last):
File "scraper.py", line 1, in <module>
import populartimes
File "../site-packages/populartimes/__init__.py", line 4, in <module>
from .crawler import run
File "../site-packages/populartimes/crawler.py", line 12, in <module>
import urllib.request
ImportError: No module named request
Does anyone know how to fix this issue?
When I run:
pip install requests
I get:
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: requests in /.../2.7/lib/python/site-packages (2.23.0)
Requirement already satisfied: idna<3,>=2.5 in /.../Python/2.7/lib/python/site-packages (from requests) (2.9)
Requirement already satisfied: chardet<4,>=3.0.2 in /.../Python/2.7/lib/python/site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/aelsabagh/Library/Python/2.7/lib/python/site-packages (from requests) (1.25.9)
Requirement already satisfied: certifi>=2017.4.17 in /.../Python/2.7/lib/python/site-packages (from requests) (2020.4.5.1)
and when I run:
pip install urllib3
I get:
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: urllib3 in /.../Python/2.7/lib/python/site-packages (1.25.9)

Flask, Bcrypt - ImportError: No module named ext.bcrypt [duplicate]

This question already has an answer here:
ImportError: No module named flaskext.sqlalchemy
(1 answer)
Closed 4 years ago.
Why the following?
from flask import Flask
from flask.ext.bcrypt import Bcrypt
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask import Flask
>>> from flask.ext.bcrypt import Bcrypt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ext.bcrypt
I have flask-bcrypt installed:
pip install Flask-Bcrypt
Requirement already satisfied: Flask-Bcrypt in /usr/local/lib/python2.7/dist-packages (0.7.1)
Requirement already satisfied: Flask in /usr/local/lib/python2.7/dist-packages (from Flask-Bcrypt) (1.0.2)
Requirement already satisfied: bcrypt in /usr/local/lib/python2.7/dist-packages/bcrypt-3.1.4-py2.7-linux-x86_64.egg (from Flask-Bcrypt) (3.1.4)
Requirement already satisfied: Jinja2>=2.10 in /usr/local/lib/python2.7/dist-packages/Jinja2-2.10-py2.7.egg (from Flask->Flask-Bcrypt) (2.10)
Requirement already satisfied: itsdangerous>=0.24 in /usr/local/lib/python2.7/dist-packages (from Flask->Flask-Bcrypt) (0.24)
Requirement already satisfied: Werkzeug>=0.14 in /usr/local/lib/python2.7/dist-packages (from Flask->Flask-Bcrypt) (0.14.1)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python2.7/dist-packages (from Flask->Flask-Bcrypt) (6.7)
Requirement already satisfied: cffi>=1.1 in /usr/local/lib/python2.7/dist-packages/cffi-1.11.5-py2.7-linux-x86_64.egg (from bcrypt->Flask-Bcrypt) (1.11.5)
Requirement already satisfied: six>=1.4.1 in /usr/local/lib/python2.7/dist-packages/six-1.10.0-py2.7.egg (from bcrypt->Flask-Bcrypt) (1.10.0)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages/MarkupSafe-1.0-py2.7-linux-x86_64.egg (from Jinja2>=2.10->Flask->Flask-Bcrypt) (1.0)
Requirement already satisfied: pycparser in /usr/local/lib/python2.7/dist-packages/pycparser-2.18-py2.7.egg (from cffi>=1.1->bcrypt->Flask-Bcrypt) (2.18)
This error leaves me nowhere to go. I followed the docs here:
https://flask-bcrypt.readthedocs.io/en/latest/
Try using from flask_bcrypt import Bcrypt.
This is because the flask documentation on flask extensions implies that extensions in the form of Flask-Foo and are imported as: from flask_foo import Foo
http://flask.pocoo.org/docs/1.0/extensions/
In the recent flask's update, from flask.ext.xxx import xxx has been removed. You should import it directly from its package. And here is a related issue.
By the way, this package seems out of date(last updates two years ago).

import flask gives ImportError: No module named jinja2

$ sudo /usr/bin/pip2 install flask==0.12
Requirement already satisfied: flask==0.12 in /usr/lib64/python2.6/site-packages
Requirement already satisfied: click>=2.0 in /usr/lib/python2.6/site-packages (from flask==0.12)
Requirement already satisfied: Jinja2>=2.4 in /usr/lib/python2.6/site-packages/Jinja2-2.6-py2.6.egg (from flask==0.12)
Requirement already satisfied: Werkzeug>=0.7 in /usr/lib/python2.6/site-packages (from flask==0.12)
Requirement already satisfied: itsdangerous>=0.21 in /usr/lib/python2.6/site-packages (from flask==0.12)
$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import flask
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/flask/__init__.py", line 19, in <module>
from jinja2 import Markup, escape
ImportError: No module named jinja2
>>>
I get requirement already satisfied during install and then I get import error during import. Why is that ?
If you don't also have a /usr/lib/python2.6/site-packages/jinja2, it's possible that a prior install got interrupted. If that's the case, in your position I'd try removing /usr/lib/python2.6/site-packages/Jinja2-2.6-py2.6.egg and doing the install of flask==0.12 again.
There's a significantly newer version of Flask available (and Python, for that matter). I assume you have reasons for wanting something older.

Keras can't be imported in Virtual Env

Any idea how this could be fixed?
mona#pascal:~$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so locally
mona#pascal:~$ source /usr/local/bin/virtualenvwrapper.sh
mona#pascal:~$ workon cv2
(cv2) mona#pascal:~$ python
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'keras'
UPDATE:
$ sudo pip install keras
The directory '/home/mona/.cache/pip/http' 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.
The directory '/home/mona/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting keras
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloading Keras-1.2.0.tar.gz (167kB)
100% |████████████████████████████████| 174kB 2.1MB/s
Collecting theano (from keras)
Downloading Theano-0.8.2.tar.gz (2.9MB)
100% |████████████████████████████████| 2.9MB 346kB/s
Requirement already satisfied: pyyaml in /usr/lib/python2.7/dist-packages (from keras)
Requirement already satisfied: six in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied: numpy>=1.7.1 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
Requirement already satisfied: scipy>=0.11 in /usr/lib/python2.7/dist-packages (from theano->keras)
Installing collected packages: theano, keras
Running setup.py install for theano ... done
Running setup.py install for keras ... done
Successfully installed keras-1.2.0 theano-0.8.2
(cv2) mona#pascal:~/computer_vision/opencv-3.2.0/build$ python
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'keras'
(cv2) mona#pascal:~/computer_vision/opencv-3.2.0/build$ sudo pip3 install keras
Requirement already satisfied (use --upgrade to upgrade): keras in /usr/local/lib/python3.4/dist-packages
Requirement already satisfied (use --upgrade to upgrade): theano in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): pyyaml in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.1 in /usr/local/lib/python3.4/dist-packages (from theano->keras)
Requirement already satisfied (use --upgrade to upgrade): scipy>=0.11 in /usr/local/lib/python3.4/dist-packages (from theano->keras)
Cleaning up...
(cv2) mona#pascal:~/computer_vision/opencv-3.2.0/build$ sudo pip3 install --upgrade keras
Requirement already up-to-date: keras in /usr/local/lib/python3.4/dist-packages
Requirement already up-to-date: theano in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already up-to-date: pyyaml in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already up-to-date: six in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already up-to-date: numpy>=1.7.1 in /usr/local/lib/python3.4/dist-packages (from theano->keras)
Requirement already up-to-date: scipy>=0.11 in /usr/local/lib/python3.4/dist-packages (from theano->keras)
Cleaning up...
(cv2) mona#pascal:~/computer_vision/opencv-3.2.0/build$ python
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'keras'
The problem is that you are running pip with sudo from the virtualenv. This temporarily makes you root, resets all the environment variables that are set by virtualenvwrapper and thus installs the packages system-wide instead in the virtualenv.
This should work better:
workon cv2
pip install keras tensorflow
Is it possible when creating cv2 environment, you did not create it with --system-site-packages flag?
If not, you can always pip install keras inside the virtualenv.

Categories