Keras can't be imported in Virtual Env - python

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.

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.

Importing Requests not Working Python 3.7

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 ...

boto3 python 2.7 ImportError: No module named boto3 USER_BASE USER_SITE site packages doesn't exist

Why python is not finding boto3 when pip or pip3 shows it's installed.
I'm on Mac machine. Tried pip/pip3 with / without sudo. Tried changing the PATH variable as well with few paths but no luck.
PATH: /Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/arun/.sdkman/candidates/gradle/current/bin:/Users/arun/aks/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
and
$ python -c 'import sys; print sys.path'|tr ',' '\012'
[''
'/Library/Python/2.7/site-packages/RBTools-0.5.7-py2.7.egg'
'/Library/Python/2.7/site-packages/wfawsclitools-0.1-py2.7.egg'
'/Library/Python/2.7/site-packages/PyYAML-3.12-py2.7-macosx-10.12-intel.egg'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload'
'/Library/Python/2.7/site-packages'
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python'
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
and
$ python -m site
sys.path = [
'/Users/arun/ubuntu-vagrant-box',
'/Library/Python/2.7/site-packages/RBTools-0.5.7-py2.7.egg',
'/Library/Python/2.7/site-packages/wfawsclitools-0.1-py2.7.egg',
'/Library/Python/2.7/site-packages/PyYAML-3.12-py2.7-macosx-10.12-intel.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Python/2.7/site-packages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
]
USER_BASE: '/Users/arun/Library/Python/2.7' (doesn't exist)
USER_SITE: '/Users/arun/Library/Python/2.7/lib/python/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
Steps I ran so far:
[arun#ip-10.11.33-65 ~/aks] $ pip install --upgrade pip
Requirement already up-to-date: pip in /usr/local/lib/python2.7/site-packages
[arun#ip-10.11.33-65 ~/aks] $ pip install boto3
Requirement already satisfied: boto3 in /usr/local/lib/python2.7/site-packages
Requirement already satisfied: botocore<1.6.0,>=1.5.0 in /usr/local/lib/python2.7/site-packages (from boto3)
Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in /usr/local/lib/python2.7/site-packages (from boto3)
Requirement already satisfied: s3transfer<0.2.0,>=0.1.10 in /usr/local/lib/python2.7/site-packages (from boto3)
Requirement already satisfied: docutils>=0.10 in /usr/local/lib/python2.7/site-packages (from botocore<1.6.0,>=1.5.0->boto3)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python2.7/site-packages (from botocore<1.6.0,>=1.5.0->boto3)
Requirement already satisfied: futures<4.0.0,>=2.2.0; python_version == "2.6" or python_version == "2.7" in /usr/local/lib/python2.7/site-packages (from s3transfer<0.2.0,>=0.1.10->boto3)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python2.7/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.6.0,>=1.5.0->boto3)
[arun#ip-10.11.33-65 ~/aks] $ pip3 install boto3
Requirement already satisfied (use --upgrade to upgrade): boto3 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): s3transfer<0.2.0,>=0.1.0 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from boto3)
Requirement already satisfied (use --upgrade to upgrade): botocore<1.5.0,>=1.4.1 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from boto3)
Requirement already satisfied (use --upgrade to upgrade): jmespath<1.0.0,>=0.7.1 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from boto3)
Requirement already satisfied (use --upgrade to upgrade): docutils>=0.10 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from botocore<1.5.0,>=1.4.1->boto3)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil<3.0.0,>=2.1 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from botocore<1.5.0,>=1.4.1->boto3)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.5.0,>=1.4.1->boto3)
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[arun#ip-10.11.33-65 ~/aks] $ which python
/usr/bin/python
[arun#ip-10.11.33-65 ~/aks] $ `which python` --version
Python 2.7.10
[arun#ip-10.11.33-65 ~/aks] $ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named boto3
>>> ^D
[arun#ip-10.11.33-65 ~/aks] $
On the other side, if I run python3, it works for import boto3 but not import boto.
Install like this:
python -m pip install --user boto3
I ran into this same issue the other day. I had python 2.7 installed on my Mac (there by default I presume). I then installed homebrew and used it to get the latest version of python setup: brew install python
Then, I had two versions of python, which is okay. Brew installed pip3 along with Python 3.6.5.
So then, I was able to run this to get boto3: pip3 install boto3
To ensure that it is accessible, use the python3 command, not the older python. So in my case, I ran my code with:
python3 signURL.py
Just in case this worked for me after upgrading to ansible 2.4.0.0:
pip install --upgrade --user boto3
That recreated my $HOME/Library/Python/2.7/lib/python/site-packages
so I'm an uber noob at python, doing something for work but posting just in case this helps someone... I had the same error and all it was was that I had everything downloaded under python3 and I was typing the python some_script.py causing the:
File "some_script.py", line 4, in <module>
import boto3
changing it to python3 some_script.py fixed it/found the proper module

Installing prettytensor for TensorFlow not working on OSX

I can't install prettytensor. I've tried doing it through pip and also by installing the sourcecode and doing python setup.py install in the directory.
I updated my tensorflow . Does anyone have any idea on how I can install prettytensor on my machine?
pip install prettytensor
Requirement already satisfied (use --upgrade to upgrade): prettytensor in ./anaconda/lib/python2.7/site-packages/prettytensor-0.6.0-py2.7.egg
Requirement already satisfied (use --upgrade to upgrade): enum34>=1.0.0 in ./anaconda/lib/python2.7/site-packages (from prettytensor)
Requirement already satisfied (use --upgrade to upgrade): six>=1.10.0 in ./anaconda/lib/python2.7/site-packages (from prettytensor)
Collecting tensorflow>=0.7.1 (from prettytensor)
Could not find a version that satisfies the requirement tensorflow>=0.7.1 (from prettytensor) (from versions: )
No matching distribution found for tensorflow>=0.7.1 (from prettytensor)
when i try to import prettytensor
Python 2.7.11 |Anaconda 2.3.0 (x86_64)| (default, Dec 6 2015, 18:57:58)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import tensorflow as tf
>>> import prettytensor as pt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Mu/anaconda/lib/python2.7/site-packages/prettytensor-0.6.0-py2.7.egg/prettytensor/__init__.py", line 25, in <module>
from prettytensor import funcs
File "/Users/Mu/anaconda/lib/python2.7/site-packages/prettytensor-0.6.0-py2.7.egg/prettytensor/funcs.py", line 25, in <module>
from prettytensor.pretty_tensor_image_methods import *
File "/Users/Mu/anaconda/lib/python2.7/site-packages/prettytensor-0.6.0-py2.7.egg/prettytensor/pretty_tensor_image_methods.py", line 20, in <module>
from prettytensor import layers
ImportError: cannot import name layers
Please check a version of tensorflow.
If your tensorflow is not 0.7.1, you can upgrade like below.
(see also https://www.tensorflow.org/versions/r0.7/get_started/os_setup.html)
# Mac OSX, CPU only:
$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.7.1-cp27-none-any.whl
Then install prettytensor again.
$ pip uninstall prettytensor
$ pip install prettytensor
For your reference.
Here is my results.(OSX 10.11.3)
In [1]: import tensorflow as tf
In [2]: tf.__version__
Out[2]: '0.7.1'
In [3]: import prettytensor as pt
In [4]: pt.PrettyTensor
Out[4]: prettytensor.pretty_tensor_class.PrettyTensor

really simple pip python-twitter issue; can't import twitter

I don't know what I'm doing wrong here:
Sun Oct 14$ pip install python-twitter
Requirement already satisfied (use --upgrade to upgrade): python-twitter in /Library/Python/2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from python-twitter)
Requirement already satisfied (use --upgrade to upgrade): simplejson in /Library/Python/2.7/site-packages (from python-twitter)
Requirement already satisfied (use --upgrade to upgrade): oauth2 in /Library/Python/2.7/site-packages (from python-twitter)
Requirement already satisfied (use --upgrade to upgrade): httplib2 in /Library/Python/2.7/site-packages (from oauth2->python-twitter)
Cleaning up...
Sun Oct 14$ python
Python 2.7.2 (default, Nov 17 2011, 13:22:48)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named twitter
>>>
Do I need to do a virtualenv? What else could be going on? Sorry for my lack of understanding but any help is appreciated.
edit #1
trying to get tweepy working but.... possibly might have two versions of Python 2.7 here
Sun Oct 14$ pip install --upgrade tweepy
Requirement already up-to-date: tweepy in ./tweepy-1.11-py2.7.egg
Cleaning up...
Sun Oct 14$ python
Python 2.7.2 (default, Nov 17 2011, 13:22:48)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tweepy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named tweepy
>>>
I think I might have two versions of Python 2.7 installed; one via homebrew and the Apple default. Is it possible that the homebrew install would put packages in /Library/Python/2.7/site-packages?
again, thx in advance
From what I've read, python-twitter:
Relies on Basic-Auth which Twitter switched off sometime between
August and September (2010). Only OAuth is supported by the API, and
python-twitter doesn't support this.
UPDATE: I just tried installing python-twitter using the exact same method you used and I am unable to import it as well. After doing some research I came across Python Twitter Tools, which I believe is python-twitter's replacement.
Installing twitter requires setuptools. It's just easy_install twitter to install it from the web.

Categories