Cant run tensorflow on ubuntu 16.04 using python 2.7 - python-2.7

I have installed the tensorflow using pip in python 2.7.
When I try to test the tensorflow in python, all I get is this message:
ubuntu#ubuntu:~$ python
Python 2.7.15 |Anaconda, Inc.| (default, May 1 2018, 23:32:55)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/anto/anaconda2/lib/python2.7/site-packages/tensorflow/__init__.py", line 28, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "/home/anto/anaconda2/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 52, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "/home/anto/anaconda2/lib/python2.7/site-packages/tensorflow/core/framework/graph_pb2.py", line 7, in <module>
from google.protobuf import descriptor as _descriptor
File "/home/anto/anaconda2/lib/python2.7/site-packages/google/protobuf/descriptor.py", line 113
class DescriptorBase(metaclass=DescriptorMetaclass):
^
SyntaxError: invalid syntax
>>>
I am currently running on:
ubuntu 16.04
ros1 kinematic
python 2.7
keras 2.6
tensorflow 1.14
protobuf 3.18.0

i have tried re-install a protobuf version of python 2.7 and tensorflow 1.x
this is all i get to solve this problem
encontered error in Tensorflow 1.8 Request you to uninstall by using
pip uninstall protobuf and try installing pip install protobuf==3.8
encontered error in Tensorflow 1.14 Request you to uninstall by using
pip uninstall protobuf and try installing pip install protobuf==3.9
it works for me

Related

ImportError: No module named rest_framework on running collectstatic

I found many questions with the same error, but none were addressing the actual cause of my problem so I am posting this. I am facing the issue in my (digital ocean) Linux production server.
I have python 3.5.2 in virtualenv, and python2.7.12 in the machine.
I have installed djangorestframework in virtualenv using command pip install djangorestframework
But did not install it in the actual machine (on 2.7)
python3 in the venv is working and importing rest_framework fine:
(venv) myname#server:/www/site$ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rest_framework
>>> exit()
python2 in the machine is working and not importing rest_framework because its not installed.
myname#server:/www/site$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rest_framework
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named rest_framework
>>> exit()
On running collectstatic command inside virtualenv I am getting the following error. It seems to be looking for the package in python2.7 and not inside virtualenv.
(venv) myname#server:/www/site$ sudo python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named rest_framework
My questing is that, Is it necessary for the package to be present in the server machine outside venv also?
If so, why? and Should I install rest_framework in the server on py2.7? If I do so, could it possibly break something?
(venv) myname#server:/www/site$ sudo python manage.py collectstatic
The issue is with the use of sudo here. It discards the environment to use the target account's one therefore discarding your virtual env.
If you want to keep it, you should likely use the -E flag to preserve the environment:
(venv) myname#server:/www/site$ sudo -E python manage.py collectstatic
you could also call the virtual env's python directly as well which would free you from setting up the visual env beforehand:
(venv) myname#server:/www/site$ which python
<path-to-python>
(venv) myname#server:/www/site$ sudo <path-to-python> manage.py collectstatic

Ruamel.yaml.jinja2: typ "jinja2"not recognised on CentOS

I'm writing a script in python to update some YAML config files containing jinja2. I found this answer showing how to do it using ruamel.yaml and ruamel.yaml.jinja2 packages and it works absolutely fine on windows10 using this configuration:
$ python -V
Python 2.7.5
$ python -m pip list
[...]
ruamel.ordereddict 0.4.13
ruamel.yaml 0.15.94
ruamel.yaml.jinja2 0.2.2
[...]
and this code:
from ruamel.yaml import YAML
yamlLoader = YAML(typ='jinja2')
But when I try to use it on a CentOS virtual machine, which is the target environment for this script, I get this error:
File "/opt/salt/mig/cnamts_migrate.py", line 17, in <module>
yamlLoader = YAML(typ='jinja2')
File "/usr/lib64/python2.7/site-packages/ruamel/yaml/main.py", line 138, in __init__
'typ "{}"not recognised (need to install plug-in?)'.format(self.typ)
NotImplementedError: typ "jinja2"not recognised (need to install plug-in?)
I can't find any difference in neither Python configuration nor packages' version.
On CentOS VM:
$ python -V
Python 2.7.5
$ pip list
Package Version
---------------------------- -----------
pip 19.1
ruamel.ordereddict 0.4.13
ruamel.yaml 0.15.94
ruamel.yaml.jinja2 0.2.2
setuptools 41.0.1
This path
/usr/lib64/python2.7/site-packages/ruamel/yaml/main.py
in the error message gives me the impression that you are using a system wide Python installation. You should (IMHO even in a VM) use python virtualenv for any utility (and preferaby one per non-related utility). You don't show the output of pip for your CentOS machine, which is way more interesting than the one on your Windows box.
$ more /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
$ /opt/python/3.7/bin/python -m venv /tmp/so-55900745
$ source /tmp/so-55900745/bin/activate
(so-55900745) $ pip install ruamel.yaml.jinja2
Collecting ruamel.yaml.jinja2
Downloading https://files.pythonhosted.org/packages/4f/b4/9676d4fa53d921f98f40dcda2ecfdb9fba2b68fbdccec3d9d4d2c87d96a7/ruamel.yaml.jinja2-0.2.2-py2.py3-none-any.whl
Collecting ruamel.yaml>=0.15.10 (from ruamel.yaml.jinja2)
Downloading https://files.pythonhosted.org/packages/bb/e3/8c06f90dab796bd5baf5da2482cf919bab3145389196814ec3180d4c7bd5/ruamel.yaml-0.15.94-cp37-cp37m-manylinux1_x86_64.whl (647kB)
100% |████████████████████████████████| 655kB 9.3MB/s
Installing collected packages: ruamel.yaml, ruamel.yaml.jinja2
Successfully installed ruamel.yaml-0.15.94 ruamel.yaml.jinja2-0.2.2
(so-55900745) $ pip list
Package Version
------------------ -------
pip 19.1
ruamel.yaml 0.15.94
ruamel.yaml.jinja2 0.2.2
setuptools 40.8.0
(so-55900745) $ python
Python 3.7.3 (default, Apr 3 2019, 11:33:06)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ruamel.yaml import YAML
>>> yamlLoader = YAML(typ='jinja2')
>>> exit()
(so-55900745) $ deactivate
$ rm -rf /tmp/so-55900745/
$ virtualenv -p /opt/python/2.7/bin/python /tmp/so-55900745
Running virtualenv with interpreter /opt/python/2.7/bin/python
New python executable in /tmp/so-55900745/bin/python
Installing setuptools, pip, wheel...
done.
$ source /tmp/so-55900745/bin/activate
(so-55900745) $ pip install ruamel.yaml.jinja2
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting ruamel.yaml.jinja2
Using cached https://files.pythonhosted.org/packages/4f/b4/9676d4fa53d921f98f40dcda2ecfdb9fba2b68fbdccec3d9d4d2c87d96a7/ruamel.yaml.jinja2-0.2.2-py2.py3-none-any.whl
Collecting ruamel.yaml>=0.15.10 (from ruamel.yaml.jinja2)
Downloading https://files.pythonhosted.org/packages/a2/59/e8cb144511e47e068efdb71a85f35d00b32fc5f05a9e9a17df265ec252b5/ruamel.yaml-0.15.94-cp27-cp27mu-manylinux1_x86_64.whl (600kB)
|████████████████████████████████| 604kB 2.0MB/s
Collecting ruamel.ordereddict; platform_python_implementation == "CPython" and python_version <= "2.7" (from ruamel.yaml>=0.15.10->ruamel.yaml.jinja2)
Downloading https://files.pythonhosted.org/packages/f3/2c/fa6d75dc459b371ed3b88fdbf8042785ce1655073c884fd97bdbb9f48e01/ruamel.ordereddict-0.4.13-cp27-cp27mu-manylinux1_x86_64.whl (99kB)
|████████████████████████████████| 102kB 12.7MB/s
Installing collected packages: ruamel.ordereddict, ruamel.yaml, ruamel.yaml.jinja2
Successfully installed ruamel.ordereddict-0.4.13 ruamel.yaml-0.15.94 ruamel.yaml.jinja2-0.2.2
(so-55900745) $ python
Python 2.7.15 (default, Aug 10 2018, 11:41:46)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ruamel.yaml import YAML
>>> yamlLoader = YAML(typ='jinja2')
>>> exit()
(so-55900745) $ deactivate
I ran into the same error message. but with python3.6.
The fix for me was to pip instead of easy_install.
Here's the easy_install related output:
(env) $ easy_install -q ruamel.yaml.jinja2
(env) $ easy_install -q ruamel.yaml
(env) $ 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.
>>> from ruamel.yaml import YAML
>>> yamlLoader = YAML(typ='jinja2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "REDACTED/env/lib/python3.6/site-packages/ruamel.yaml-0.15.96-py3.6-linux-x86_64.egg/ruamel/yaml/main.py", line 138, in __init__
'typ "{}"not recognised (need to install plug-in?)'.format(self.typ)
NotImplementedError: typ "jinja2"not recognised (need to install plug-in?)
Here's the same thing installing with pip:
(env) $ pip -q install ruamel.yaml.jinja2
(env) $ pip -q install ruamel.yaml
(env) $ 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.
>>> from ruamel.yaml import YAML
>>> yamlLoader = YAML(typ='jinja2')
>>> print("¯\_(ツ)_/¯")
¯\_(ツ)_/¯
If you are using a setup.py in your project, check out Can I use `pip` instead of `easy_install` for `python setup.py install` dependency resolution?

Can't import a module in python 2.7 even though pip says it is installed

I've read the many posts on SO regarding using Pillow or Image instead of PIL but I'm still having trouble with this module.
I'm running this on my MBP on 10.9.5.
I used pip to install image 1.5.5 and Pillow 3.4.2. However, when I go to import the module in my script, it keeps saying there's no module of that name.
first.last#localhost:/usr/local/bin> pip freeze | grep Pillow
Pillow==3.4.2
first.last#localhost:/usr/local/bin> pip freeze | grep image
image==1.5.5
first.last#localhost:/usr/local/bin> which pip
/usr/local/bin/pip
first.last#localhost:/usr/local/bin> which python
/usr/local/bin/python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named image
>>> import PIL
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL
>>> import Pillow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Pillow
>>> import pillow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pillow
I checked /Library/Python/2.7/site-packages and it doesn't seem like the .py files are there for pillow or image, even though pip freeze showed that image and Pillow are both installed.
This is the contents of pip is this. Not sure if it gives any clues to why pip thinks the modules are installed but python says it can't find it.
#!/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==9.0.1','console_scripts','pip'
__requires__ = 'pip==9.0.1'
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('pip==9.0.1', 'console_scripts', 'pip')()
)
From what I showed in my setup/environment, can anyone point me in a general direction to start fixing this problem? Is any additional information needed to debug this some more?
Since you are working outside of a virtualenv, the user site should be enabled. Use the following commands to find the installed location of image and/or pillow:
pip show -f image | grep Location
pip show -f pillow | grep Location
What you probably want to see is something like:
Location: /Users/<your_username>/.local/lib/python2.7/site-packages
If it's not installing there, then uninstall and pip install again with the --user flag.
Then use the following command to make sure that the results of above will be found in your path:
python -m site
What you want to see in the output is something like
sys.path = [
...
]
USER_BASE: '/Users/<your_username>/.local' (exists)
USER_SITE: '/Users/<your_username>/.local/lib/python2.7/site-packages' (exists)
ENABLE_USER_SITE: True
If the installed location is in your user site, and your user site is enabled, then the import will work.

Can not import libxml2 and libxslt in Python 2.7 and CentOS 7

Can not import libxml2 and libxslt in Python 2.7 and CentOS 7.
But system says that these packages are installed:
Package libxml2-2.9.1-5.el7_1.2.x86_64 already installed and latest version
Package libxml2-devel-2.9.1-5.el7_1.2.x86_64 already installed and latest version
Package libxslt-1.1.28-5.el7.x86_64 already installed and latest version
Package libxslt-devel-1.1.28-5.el7.x86_64 already installed and latest version
But can not import in Python2.7
Python 2.7.5 (default, Jun 24 2015, 00:41:19)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import libxml2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named libxml2
>>> import libxslt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named libxslt
>>>
libxml2 install step
yum groupinstall "Development Tools"
yum install python-devel.x86_64
rpmbuild -ba /usr/src/redhat/SPECS/libxml2.spec (this might not be required.)
##go to RPMS/x86_64/
rpm -Uvh libxml2-python-2.7.8-1.x86_64.rpm --nodeps
===================================================================
libxslt install step
wget http://xmlsoft.org/sources/libxslt-1.1.28.tar.gz
cd libxslt-1.1.28
./configure --with-python=/usr/bin/python2.7
make
make install
========================================

Confusion regarding python 2.6/2.7 package installations

I have a Redhat 6 box which originally had python 2.6 installed on it (invoked by /usr/bin/python). A few days ago I installed 2.7.10 (invoked by /usr/local/bin/python or simply python).
Earlier today I installed pip using sudo easy_install pip. The result of whereis pip is pip: /usr/bin/pip2.6 /usr/bin/pip
Then I wanted to install the pandas package by running sudo pip install pandas. It reported success but import pandas produces an error for both versions of python:
myPrompt 733] python
Python 2.7.10 (default, Jul 16 2015, 14:41:11)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
>>> exit()
myPrompt 734] /usr/bin/python
Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
>>> exit()
myPrompt 735] pip install pandas
Traceback (most recent call last):
File "/usr/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 2655, in <module>
working_set.require(__requires__)
File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 648, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 546, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: pip==7.1.0
myPrompt 736] sudo pip install pandas
Enter PASSCODE:
Requirement already satisfied (use --upgrade to upgrade): pandas in /usr/lib64/python2.6/site-packages
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/lib/python2.6/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): pytz>=2011k in /usr/lib/python2.6/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.0 in /usr/lib64/python2.6/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/lib/python2.6/site-packages (from python-dateutil->pandas)
So there is some sort of confusion with pip configuration and how packages get installed. How can I untangle this? Do I need to reinstall pip?
Ultimately, I don't care about python 2.6 and only want to work with 2.7
Thx
Looks like the reason was that I installed the packages as root. Doing
sudo chmod -R ugo+rX /usr/local/lib/python2.7/site-packages
fixed the problem
For reference, this can happen when you have multiple Python installations, for example via brew. In that case remove one:
brew remove python --force
This one fixed for me.
This case can be fixed using the steps provided in the following answer: Easy_install and pip broke: pkg_resources.DistributionNotFound: distribute==0.6.36
It simply replaces the current pip install (which refers to the new/wrong python environment) with a fresh/correct one.