MySQL connector fails after switching from Python 2.7.X to 3.X - mysql

I installed the following mysql connector via pip:
pip install --allow-external mysql-connector-python mysql-connector-python
In my code I simply do a import mysql.connector as Connector and everything works fine. However, I used to use Python 2.7.X and now want to switch to Python 3.X (currently 3.5).
When I run my stuff under 2.7.X everything works fine. However, starting the same script with python 3.5 I get the following error message:
Traceback (most recent call last):
File "SWCrawler3.py", line 3, in <module>
import Configuration as cfg
File "/Users/Tom/SW/SWCrawler/SWCrawler/Configuration.py", line 4, in <module>
from database.MySQLConnection import MySQLConnection as MySQLConn
File "/Users/Tom/SW/SWCrawler/SWCrawler/database/MySQLConnection.py", line 6, in <module>
import mysql.connector as Connector
File "../mysql/connector/__init__.py", line 33, in <module>
from mysql.connector.connection import MySQLConnection
File "../mysql/connector/connection.py", line 128
except Exception, err:
^
SyntaxError: invalid syntax
Please note, that I wrote a wrapper file that I call MySQLConnection.

I had to intall the python3 packages of mysql-connector-python.
On my Mac I did the following:
brew install python3
pip3 install --allow-external mysql-connector-python mysql-connector-python
After that is worked. So use pip3.

On Windows with Python 3.5, I did something very close to toom's answer, that is :
pip3.5 install --allow-external mysql-connector-python mysql-connector-python

Related

Openstack ImportError: cannot import name 'joinedload_all' from 'sqlalchemy.orm'

I am trying to install Openstack(Victoria) on my ubuntu 20.04 and getting below error.
File "/usr/local/bin/cinder-manage", line 6, in <module>
from cinder.cmd.manage import main
File "/opt/stack/cinder/cinder/cmd/manage.py", line 75, in <module>
from cinder.db import migration as db_migration
File "/opt/stack/cinder/cinder/db/migration.py", line 26, in <module>
from cinder.db.sqlalchemy import api as db_api
File "/opt/stack/cinder/cinder/db/sqlalchemy/api.py", line 45, in <module>
from sqlalchemy.orm import joinedload, joinedload_all, undefer_group, load_only
ImportError: cannot import name 'joinedload_all' from 'sqlalchemy.orm' (/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/__init__.py)
'```
I have tried installing using ```pip install sqlalchemy.orm ```
joinedload_all was removed in 1.4 release notes
You can try capping the version to less than 1.4 like this:
pip install 'sqlalchemy<1.4'
I'm not sure if openstack uses a virtualenv but you probably will want to install its dependencies in something like.
You should always be installing requirements with constraints applied for the version you are deploying.
pip install -c https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt?h=stable/victoria sqlalchemy
This will ensure that you are installing the recommended version for the OpenStack release you are using.
You could also try to just re-install all the requirements for cinder with the correct versions.
cd /opt/stack/cinder pip install -c https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt?h=stable/victoria /opt/stack/cinder/requirements.txt

AttributeError: '_NamespacePath' object has no attribute 'sort' on Read the docs

Pretty sure this isn't a dupe.
my read docs build fails here:
Collecting sphinxcontrib-inlinesyntaxhighlight==0.2 (from -r /home/docs/checkouts/readthedocs.org/user_builds/zproc/checkouts/latest/requirements.txt (line 33))
Using cached https://files.pythonhosted.org/packages/5c/8b/17998fe9ebbec7e176f344648a86094d7b358e63d8f4593ae3dfbb9bb560/sphinxcontrib-inlinesyntaxhighlight-0.2.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/setuptools/__init__.py", line 10, in <module>
from setuptools.extern.six.moves import filter, filterfalse, map
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/setuptools/extern/__init__.py", line 1, in <module>
from pkg_resources.extern import VendorImporter
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/pkg_resources/__init__.py", line 3017, in <module>
#_call_aside
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
f(*args, **kwargs)
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/pkg_resources/__init__.py", line 3045, in _initialize_master_working_set
dist.activate(replace=False)
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2577, in activate
declare_namespace(pkg)
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2151, in declare_namespace
_handle_ns(packageName, path_item)
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2091, in _handle_ns
_rebuild_mod_path(path, packageName, module)
File "/home/docs/checkouts/readthedocs.org/user_builds/zproc/envs/latest/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2120, in _rebuild_mod_path
orig_path.sort(key=position_in_sys_path)
AttributeError: '_NamespacePath' object has no attribute 'sort'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-wlv3e5i4/sphinxcontrib-inlinesyntaxhighlight/
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Looks like it is failing to install sphinxcontrib-inlinesyntaxhighlight, which installs just fine in my local machine.
A simple search seems to say that it can be fixed using pip install --upgrade pip setuptools, but I don't know how to do that on read the docs.
Here is the requirements.txt i supply to read the docs
alabaster==0.7.11
Babel==2.6.0
backcall==0.1.0
certifi==2018.4.16
chardet==3.0.4
decorator==4.3.0
docutils==0.14
idna==2.7
imagesize==1.0.0
ipython==6.4.0
ipython-genutils==0.2.0
jedi==0.12.1
Jinja2==2.10
MarkupSafe==1.0
packaging==17.1
parso==0.3.0
pexpect==4.6.0
pickleshare==0.7.4
pkginfo==1.4.2
prompt-toolkit==1.0.15
psutil==5.4.6
ptyprocess==0.6.0
Pygments==2.2.0
pyparsing==2.2.0
pytz==2018.5
pyzmq==17.0.0
requests==2.19.1
requests-toolbelt==0.8.0
simplegeneric==0.8.1
six==1.11.0
snowballstemmer==1.2.1
Sphinx==1.7.5
sphinxcontrib-inlinesyntaxhighlight==0.2
sphinxcontrib-websupport==1.1.0
tblib==1.3.2
tinydb==3.9.0.post1
tqdm==4.23.4
traitlets==4.3.2
twine==1.11.0
urllib3==1.23
wcwidth==0.1.7
As suggested here. and quoting the same -
The _path_ attribute should always be treated as an interable, not a list (the Python docs describe _path_ as an iterable). The line in pkg_resources/_init_.py that reads:
orig_path.sort(key=position_in_sys_path)
Should be:
orig_path = sorted(orig_path, key=position_in_sys_path)
As a general rule (if practical), in-place sorting should not be used.
PS -
I tried upgrading pip and setuptools but it didn't work for me.
pip install -U pip
pip install -U setuptools

Getting Error while building mysql for python on Mac OS X El Capitan

I read some of the related topics and tried to implement those solutions to my situation but still I get the following error after this command :
(project2_env) Efe-MacBook-Air:MySQL-python-1.2.4b4 efe$ python setup.py build
The Error message is :
Extracting in /var/folders/rv/vbf7xqh1601_xjkrn85w7hp00000gn/T/tmpptpsggg7
Traceback (most recent call last):
File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 143, in use_setuptools
raise ImportError
ImportError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "setup.py", line 7, in <module>
use_setuptools()
File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 145, in use_setuptools
return _do_download(version, download_base, to_dir, download_delay)
File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 125, in _do_download
_build_egg(egg, tarball, to_dir)
File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 99, in _build_egg
_extractall(tar)
File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 486, in _extractall
self.chown(tarinfo, dirpath)
TypeError: chown() missing 1 required positional argument: 'numeric_owner'
Edit: I reinstalled homebrew, then I run this following command. That is successfully installed.
brew install mysql
However, I cannot still import MySQLdb in python.
As it is more complicated than the previous answer, i won't told you about the custom installation of python mysql.
Found this, the Jude's way :
Install mysql via homebrew, then you can install mysql python via pip.
xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
//don't know how to install mysql via homebrew, but it should be done here
pip install MySQL-python
https://stackoverflow.com/a/25356073/6660122
Taken from the Python2 doc, still relevant in 3.5 :
"Alternate installation: the user scheme
This scheme is designed to be the most convenient solution for users that don’t have write permission to the global site-packages directory or don’t want to install into it. It is enabled with a simple option: "
python setup.py install --user
Could help in your case.
Notice there are different scheme to use : --home, or --prefix and --exec-prefix, or --install-base and --install-platbase
Finally I found a solution for myself and by myself. I gave up using directly MySQLdb library and instead of that, I installed PyMySQL. By the way I had already installed MySQL Workbench before, so mysql server was already running.
Here is what I do step by step. I hope someone can get benefit from it.
1) Create a virtual environment:
virtualenv virt1
2) Make your virtual environment activated:
source virt1/bin/activate
3) Now, you are in your virtual environment. Install PyMySQL:
pip install PyMySQL
4) Now, try whether your MySQL connection is OK or not with a simple py executable:
#!/usr/bin/python
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='YourDBuserName',
password='YourDBpassword',
db='YourDBname',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `name` FROM `YourTableName` WHERE `id`=%s"
cursor.execute(sql, (1,))
result = cursor.fetchone()
print(result)
finally:
connection.close()
5) You should see that your first row from the table appears on the screen.
Note: The part "import pymysql.cursors" could be tricky. First, I wrote as "import PyMySQL.cursors" and that didn't work!

Error installing NLTK in Python 3.4

I'm transitioning to python 3.4 and trying to install NLTK.
Pyyaml and numpy are installed, setuptools and pip as well - all in the newest version- Win 7 64bit.
However, NLTK puzzles me with an error no matter how I try to install it. Here is the cmd-output of my last try. Btw, haven't changed the files in any way ;)
Does anyone know what I could do to get NLTK up and running?
C:\Python34\Scripts\nltk-3.0a4>python setup.py install
running install
Traceback (most recent call last):
File "setup.py", line 81, in <module>
zip_safe=True, # since normal files will be present too?
File "C:\Python34\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python34\lib\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python34\lib\site-packages\setuptools\command\install.py", line 65, i
n run
self.do_egg_install()
File "C:\Python34\lib\site-packages\setuptools\command\install.py", line 101,
in do_egg_install
cmd.ensure_finalized() # finalize before bdist_egg munges install cmd
File "C:\Python34\lib\distutils\cmd.py", line 107, in ensure_finalized
self.finalize_options()
File "C:\Python34\lib\site-packages\setuptools\command\easy_install.py", line
304, in finalize_options
self.index_url, search_path = self.shadow_path, hosts=hosts,
File "C:\Python34\lib\site-packages\setuptools\package_index.py", line 269, in
__init__
Environment.__init__(self,*args,**kw)
File "C:\Python34\lib\site-packages\pkg_resources.py", line 799, in __init__
self.scan(search_path)
File "C:\Python34\lib\site-packages\pkg_resources.py", line 829, in scan
self.add(dist)
File "C:\Python34\lib\site-packages\pkg_resources.py", line 849, in add
dists.sort(key=operator.attrgetter('hashcmp'), reverse=True)
TypeError: unorderable types: str() < NoneType()
Sorry, sometimes its the most obvious things that work...
In this case, simply double clicking the setup.py file from the nltk folder, allowing windows to execute it with python and its installed beautifully.
Try to install it using the following command
C:\Python34\Scripts\nltk-3.0a4>setup.py install
Open Command Prompt (Run as Administrator). Use cd to navigate to the folder in which your downloaded nltk package is present. Then run the command python setup.py install. That's all.
Did you try updating the setuptools package:
pip3 install --upgrade setuptools

Error when trying to install Django CMS in virtualenv w/ MySQL on Mac OSX

I am getting the following error when I try to install django CMS with MySQL using virutalenv:
RuntimeError: maximum recursion depth exceeded
/Users/ethan/Sites/env/build/mysql-python/distribute-0.6.28-py2.7.egg
Traceback (most recent call last):
File "<string>", line 17, in <module>
File "/Users/ethan/Sites/env/build/mysql-python/setup.py", line 7, in <module>
use_setuptools()
File "distribute_setup.py", line 145, in use_setuptools
return _do_download(version, download_base, to_dir, download_delay)
File "distribute_setup.py", line 125, in _do_download
_build_egg(egg, tarball, to_dir)
File "distribute_setup.py", line 116, in _build_egg
raise IOError('Could not build the egg.')
IOError: Could not build the egg.
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /Users/ethan/Sites/env/build/mysql-python
The command I am using for the install is the following:
env/bin/pip install --download-cache=~/.pip-cache -r reqs.txt
And the reqs.txt file looks like this:
django-cms==2.4.1
Django==1.5.1
django-classy-tags==0.4
South==0.8.1
html5lib==1.0b1
django-mptt==0.5.2
django-sekizai==0.7
six==1.3.0
Pillow==2.0.0
django-filer==0.9.4
cmsplugin-filer==0.9.5
django-reversion==1.7
mysql-python==1.2.4
I'm a novice to django (just going through the tutorial) and I cannot find anything in a search that seems to resolve this issue. Anyone have any ideas?
Change your requirement to mysql-python==1.2.5, I had the same problem on a CentOS 6.3 server.
mysql-python v. 1.2.4 has a hard dependency on distribute version 0.6.28. It might not be picking the right version of distribute.
Run this after the virtual env is activated:
easy_install distribute==0.6.28
and you should be good to proceed with
env/bin/pip install --download-cache=~/.pip-cache -r reqs.txt
change requirement to mysql-python==1.2.5 also worked for me on OS X mavericks with pip install