Installing MySQLdb for python2.7 - python-2.7

I have various versions of python (python2.4 and python2.7) installed on the CentOS machine. I installed MySQL-python, but I can only import the MySQLdb module on python2.4 but not python2.7
root# python2.7
Python 2.7.11 |Continuum Analytics, Inc.| (default, Dec 6 2015, 18:08:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
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 MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
root# python
Python 2.4.3 (#1, Mar 5 2011, 21:26:05)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
I've tried alias'ing python to /usr/bin/python2.7 and then doing a 'yum remove MySQL-python' and then doing a 'yum install MySQL-python' but no luck. Any clues/insights? Thanks.

The yum will install the MySQL-python to the system default python's directory.
maybe you can try virtualenv and pip.
install the pip
pip install virtualenv
virtualenv -p /the/localtion/of/python27 python27
active the new python envirement, and pip install MySQL-python

Related

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?

How to install psycopg2 for python 2.7

While isntaling binaryanalysis Iv got an error ImportError: No module named psycopg2 but I have installed it but its only avalible for python 3.5. How I can install it for python2.7 ?
Python 3.5
$ python3.5
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> exit
Use exit()
Python 2.7
$ python2.7
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named psycopg2
>>>
Edit:
$ pip install psycopg2
Requirement already satisfied: psycopg2 in /usr/local/lib/python3.5/dist-packages
Try this
pip install psycopg2-binary
I did this for Python 2.7, not sure if it works for Python 3.6

My conda environment use default "six" package, not what I installed in conda environment

(python2.7) (23:28:22) ~$ python
Python 2.7.14 |Anaconda, Inc.| (default, Dec 7 2017, 17:05:42)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import six
>>> six.__version__
'1.5.2'
>>> quit()
(python2.7) (23:30:09) ~$ conda install six
Fetching package metadata ...........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /home/hyejinj/anaconda3/envs/python2.7:
#
six 1.11.0 py27h5f960f1_1
I need the six package with version 1.11.0, but my python in the conda environment keeps using the default six, whose version is 1.5.2. How can I resolve this issue?

Link Tensorflow from usr/bin/python to a conda environment python

I have Tensorflow with GPU installed on my main usr/bin/python, e.g. on an AWS EC2 machine and I'd like to use it within a conda environment. Is this possible without reinstalling tensorflow for each environment ?
EDIT: I found out about the virtual-env --system-site-packages option. Maybe an equivalent for Conda ? And especially for packages like Tensorflow-gpu that need to be built for the specific hardware, and are not available directly on pip.
If tensorflow is installed in the system, then it will probably be present in one of these four directories:
/usr/local/lib/python2.7/dist-packages
/usr/local/lib/python2.7/site-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/site-packages
To have the system packages also be available in conda, you can do the following:
(root) ~/condaexpts $ cat <<EOF > $CONDA_PREFIX/lib/python2.7/site-packages/systempkgs.pth
/usr/local/lib/python2.7/dist-packages
/usr/local/lib/python2.7/site-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/site-packages
EOF
Then verify that these paths have been added:
(root) ~/condaexpts $ python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
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 sys
>>> sys.path
['', '/home/ubuntu/condaexpts/m2/lib/python27.zip', '/home/ubuntu/condaexpts/m2/lib/python2.7', '/home/ubuntu/condaexpts/m2/lib/python2.7/plat-linux2', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-tk', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-old', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-dynload', '/home/ubuntu/condaexpts/m2/lib/python2.7/site-packages', '/home/ubuntu/condaexpts/m2/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/site-packages', '/usr/lib/python2.7/dist-packages']
And now you can import any system package.
If you don't want this change to be permanent for your conda environment, you can always add the directory containing system packages to PYTHONPATH environment variable:
(root) ~/condaexpts $ PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/ python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
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 sys
>>> sys.path
['', '/home/ubuntu/condaexpts', '/usr/local/lib/python2.7/dist-packages', '/home/ubuntu/condaexpts/m2/lib/python27.zip', '/home/ubuntu/condaexpts/m2/lib/python2.7', '/home/ubuntu/condaexpts/m2/lib/python2.7/plat-linux2', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-tk', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-old', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-dynload', '/home/ubuntu/condaexpts/m2/lib/python2.7/site-packages', '/home/ubuntu/condaexpts/m2/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg']

First time install Django on Mac OSX Mountain Lion

First time ever I'm installing Django (and using python). I run Mac OSX 10.8 Mountain Lion and by default, python is already installed:
$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
I've followed the steps at https://docs.djangoproject.com/en/1.4/intro/install/ namely:
Download Django at https://www.djangoproject.com/download/ and run the following:
$ tar xzvf Django-1.4.1.tar.gz
$ cd Django-1.4.1
$ sudo python setup.py install
This has created a symlink in /user/local/bin which allows me to invoke Django from anywhere with $ django-admin.py
However, trying the following fails:
$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>
I should be getting something after typing >>> import django
>>> print django.get_version()
1.4
Is it me missing something or the documentation not so accurate?
Actually, the fact that import django doesn't print anything means that Django was installed correctly.
import django only imports the Django module; it doesn't start a webserver or anything.