On Windows 10 pro, I am running a docker image with python and jupyter installed. I can start the docker image and from that shell start the jupyter notebook fine. Both python2 and python3 kernels are available. PIL and Pillow are installed but I cannot import them. See below:
(I have indicated the contents of each jupyter cell with double asterisks.
The result of each cell then simply follows below it)
**!pip install Image**
Requirement already satisfied: Image in /usr/local/lib/python3.6/dist-packages (1.5.27)
Requirement already satisfied: pillow in /usr/lib/python3/dist-packages (from Image) (5.1.0)
Requirement already satisfied: django in /usr/local/lib/python3.6/dist-packages (from Image) (2.2)
Requirement already satisfied: sqlparse in /usr/local/lib/python3.6/dist-packages (from django->Image) (0.3.0)
Requirement already satisfied: pytz in /usr/lib/python3/dist-packages (from django->Image) (2018.3)
You are using pip version 19.0.3, however version 19.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
**!pip install Pillow**
Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages (5.1.0)
You are using pip version 19.0.3, however version 19.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
**!python -m pip install pillow**
Requirement already satisfied: pillow in /usr/lib/python3/dist-packages (5.1.0)
You are using pip version 19.0.3, however version 19.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
**import PIL**
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-70d569469e06> in <module>()
----> 1 import PIL
ImportError: No module named PIL
**import PIL.Image**
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-df3f6661ddbf> in <module>()
----> 1 import PIL.Image
ImportError: No module named PIL.Image
**import pillow**
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-11-e32915389b2c> in <module>()
----> 1 import pillow
ImportError: No module named pillow
But when I exit the notebook and start an interactive python session in the running docker container, PIL is found easily:
root#7092fc2336d3:/# python
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>>
well this is weird. It turns out that the pip in jupyter was installing in the python3 site packages but the python in the jupyter notebook was looking in the python2 site packages. so when I installed pillow in a jupyter cell like so
!/usr/bin/python2.7 -m pip install Pillow
now this works
import PIL.Image
Related
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.
I am working on jupyter-notebook.
When i give the command
import pandas
df = pandas.read_csv('/home/pglab1/WiFi Strength_Mar06.csv')
print(df)
it shows the following error:
ImportError Traceback (most recent call last)
<ipython-input-39-b8728196942c> in <module>()
----> 1 import pandas
2 df = pandas.read_csv('/home/pglab1/WiFi Strength_Mar06.csv')
3 print(df)
/usr/local/lib/python2.7/dist-packages/pandas/__init__.py in <module>()
21
22 # numpy compat
---> 23 from pandas.compat.numpy import *
24
25 try:
/usr/local/lib/python2.7/dist-packages/pandas/compat/numpy/__init__.py in <module>()
20 'your numpy version is {0}.\n'
21 'Please upgrade numpy to >= 1.12.0 to use '
---> 22 'this pandas version'.format(_np_version))
23
24
ImportError: this version of pandas is incompatible with numpy < 1.12.0
your numpy version is 1.11.0.
Please upgrade numpy to >= 1.12.0 to use this pandas version
in the same notebook, when I give the command,
!pip install pandas,
the output is
Requirement already satisfied: pandas in /usr/local/lib/python2.7/dist-packages (0.24.1)
Requirement already satisfied: python-dateutil>=2.5.0 in /usr/local/lib/python2.7/dist-packages (from pandas) (2.6.1)
Requirement already satisfied: numpy>=1.12.0 in /usr/local/lib/python2.7/dist-packages (from pandas) (1.16.2)
Requirement already satisfied: pytz>=2011k in /usr/lib/python2.7/dist-packages (from pandas) (2014.10)
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/dist-packages (from python-dateutil>=2.5.0->pandas) (1.10.0)
cheetah 2.4.4 requires Markdown>=2.0.1, which is not installed.
You are using pip version 10.0.0, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
I am not familiar with software environment, so kindly help me how to correct this.( The problem is Pandas require a numpy package >1.12.0 but the numpy package in .../dist-packages is 1.16.2. this is my understanding)
This works in such situations
import sys
!{sys.executable} -m pip install numpy
Try:
pip install --upgrade pip
pip install markdown
pip install --upgrade numpy
pip install pandas
You might want to install using the following command:
import sys
!{sys.executable} -m pip install pandas
rather than just !pip install pandas - this will ensure that you are using the right pip for associated with the kernel.
Trying to install tensorflow. Tried anaconda, it worked but affected my other program. Then decided to use pip install.
But after I installed it, I just can't import it within ipython.
Here are the messages: I tried uninstall and reinstall.
pip install tensorflow
Requirement already satisfied (use --upgrade to upgrade): tensorflow in /Library/Python/2.7/site-packages
Cleaning up...
First I try to import in ipython:
In [1]: import tensorflow
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-a649b509054f> in <module>()
----> 1 import tensorflow
ImportError: No module named tensorflow
And then I copied the folder of tensorflow from /Library/Python/2.7/site-packages to /usr/local/lib/python2.7/site-packages/. I try to import:
In [1]: import tensorflow
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-a649b509054f> in <module>()
----> 1 import tensorflow
/Library/Python/2.7/site-packages/tensorflow/__init__.py in <module>()
21 from __future__ import print_function
22
---> 23 from tensorflow.python import *
24
25
/Library/Python/2.7/site-packages/tensorflow/python/__init__.py in <module>()
57 please exit the tensorflow source tree, and relaunch your python interpreter
58 from there.""" % traceback.format_exc()
---> 59 raise ImportError(msg)
60
61 from tensorflow.core.framework.node_def_pb2 import *
ImportError: Traceback (most recent call last):
File "tensorflow/python/__init__.py", line 53, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "tensorflow/core/framework/graph_pb2.py", line 6, in <module>
from google.protobuf import descriptor as _descriptor
ImportError: No module named google.protobuf
Error importing tensorflow. Unless you are using bazel,
you should not try to import tensorflow from its source directory;
please exit the tensorflow source tree, and relaunch your python interpreter
from there.
However, I have protobuf installed under /Library/Python/2.7/site-packages as well.
I find out this version of ipython is using macports python package directory:
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
I am not sure how to let ipython search for the pip installed directory.
I reinstalled the tensorflow again as it is described on the website:
sudo pip install --upgrade $TF_BINARY_URL
Password:
Downloading/unpacking https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl
Downloading tensorflow-0.11.0rc0-py2-none-any.whl (35.5MB): 35.5MB downloaded
Downloading/unpacking protobuf==3.0.0 (from tensorflow==0.11.0rc0)
Downloading protobuf-3.0.0-py2.py3-none-any.whl (342kB): 342kB downloaded
Requirement already up-to-date: wheel in /Library/Python/2.7/site-packages (from tensorflow==0.11.0rc0)
Requirement already up-to-date: mock>=2.0.0 in /Library/Python/2.7/site-packages (from tensorflow==0.11.0rc0)
Requirement already up-to-date: numpy>=1.11.0 in /Library/Python/2.7/site-packages (from tensorflow==0.11.0rc0)
Requirement already up-to-date: six>=1.10.0 in /Library/Python/2.7/site-packages (from tensorflow==0.11.0rc0)
Downloading/unpacking setuptools from https://pypi.python.org/packages/be/20/3f4d2fb59ddeed35532bd4e11e900abcf8019d29f4558d38169639303536/setuptools-28.2.0-py2.py3-none-any.whl#md5=02e79b1127c5a131a2dace6d30cf7f25 (from protobuf==3.0.0->tensorflow==0.11.0rc0)
Downloading setuptools-28.2.0-py2.py3-none-any.whl (467kB): 467kB downloaded
Installing collected packages: tensorflow, protobuf, setuptools
Found existing installation: protobuf 3.1.0.post1
Uninstalling protobuf:
Successfully uninstalled protobuf
Found existing installation: setuptools 18.5
Uninstalling setuptools:
Successfully uninstalled setuptools
Successfully installed tensorflow protobuf setuptools
Cleaning up...
And still no luck:
In [1]: import tensorflow
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-a649b509054f> in <module>()
----> 1 import tensorflow
OK, I got the problem fixed.
I am not sure what exactly is causing it. Because I installed all this on my RMBP, nothing wrong. But desktop just won't take it.
Solution is:
Uninstall all previously installed or half installed tensorflow, and my other packages (just two main packages). And use anaconda to install tensorflow, activate it. And use anaconda to install my other packages.
Then problem solved.
I've installed the protobuf package but I'm unable to import it.
> pip uninstall protobuf
... uninstalls
> pip install protobuf
... installs. confirm that it's installed:
pip install protobuf
Requirement already satisfied (use --upgrade to upgrade): protobuf in ./.venv/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./.venv/lib/python3.5/site-packages (from protobuf)
Requirement already satisfied (use --upgrade to upgrade): six>=1.9 in ./.venv/lib/python3.5/site-packages (from protobuf)
Back in ipython:
In [1]: from google.protobuf import descriptor as _descriptor
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-3baca6afb060> in <module>()
----> 1 from google.protobuf import descriptor as _descriptor
ImportError: No module named google.protobuf
In [2]:
I'm really stumped.
> python --version
> Python 3.5.1
> which pip
/Users/skyler/work/TemplateProcessor/.venv/bin/pip
> pip --version
pip 8.1.2 from /Users/skyler/work/TemplateProcessor/.venv/lib/python3.5/site-packages (python 3.5)
I had the same issue and found a workaround suggested in this issue:
1.Adding a new file tensorflow/google/init.py:
try:
import pkg_resources
pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
Add namespace_packages to tensorflow/google/protobuf/python/setup.py:
setup(
name='protobuf',
namespace_packages=['google'],
...
)
Recompile / pip install. Good luck!
I run the following command to install pandas via pip:
sudo pip install pandas --upgrade
which outputs
Requirement already up-to-date: pandas in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
Requirement already up-to-date: numpy>=1.7.0 in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (from pandas)
Requirement already up-to-date: python-dateutil>=2 in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (from pandas)
Requirement already up-to-date: pytz>=2011k in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (from pandas)
Requirement already up-to-date: six>=1.5 in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (from python-dateutil>=2->pandas)
However, when I use python3 in the command line, I cannot import pandas:
$ python3
>>> import pandas
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pandas'
It appears that this is in the correct location, as
which python3
is in the following location:
/opt/local/bin/python3
Executing within python3
>>> import sys
>>> print(sys.version)
outputs
'3.4.5 (default, Jun 27 2016, 04:57:21) \n[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]'
Why can't I import pandas?
EDIT: I'm using pip version pip3:
pip --version
outputs
pip 8.1.2 from /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)
Looks like your OS uses pip2 by default. This could be checked by typing:
$ pip --version
pip 8.1.2 from /usr/local/lib/python2.7/dist-packages (python 2.7)
Try to use pip3 command like that:
sudo pip3 install pandas --upgrade