Symbol not found: _sqlite3_enable_load_extension - sqlite installed via homebrew - python

Symptom: In my Django app, when I call from pysqlite2._sqlite import * I get the traceback Symbol not found: _sqlite3_enable_load_extension when
Background:
I've installed python using homebrew (python 2.7.13), which auto installed sqlite
I am running macOS 10.12.3 with Command Line Tools macOS 10.12, Xcode 8.2.1
I've installed pysqlite using pip (pysqlite 2.8.3)
I have tried
brew uninstall sqlite and brew uninstall python and reinstalling
Adding these to my .bash_profile
export PATH="$(brew --prefix sqlite)/bin:$PATH"
LDFLAGS="-L/usr/local/opt/sqlite/lib"
CPPFLAGS="-I/usr/local/opt/sqlite/include"
export PKG_CONFIG_PATH=“/usr/local/opt/sqlite/lib/pkgconfig”
python -c "import sqlite3" doesn't return any errors
Gist of traceback: https://gist.github.com/xwchen/e9704fa34f0463d2117fe9fbb37922a1

Copied answer from here (https://github.com/Homebrew/homebrew-core/pull/3134). If you're like me, you probably installed sqlite after you install python3. Anyway, if anyone stumbles upon this question and needs an answer...
First remove SQLite, python and python3 if installed via homebrew
brew uninstall --force python
brew uninstall --force python3
brew uninstall --force sqlite
This removes all copies.
Then make sure Xcode 8 or later is installed
Important
Re-install command line tools via
xcode-select --install
sudo xcode-select --reset
and finally install command line tools via pkg file found here.
Look for command line tools in search. (I've downloaded "Command Line Tools (macOS 10.12) for Xcode 8.dmg") Then open DMG and install the pkg found there.
Now install SQLite and then python and python 3 if needed:
brew install sqlite
brew install python
brew install python3
Run brew install python --verbose and make sure there are no warnings, if none then issues should be resolved.
Don't forget to do
pip install setuptools --upgrade && pip3 install setuptools --upgrade

It may happen because it's not able to locate the .so library. As explained here:
On OS X, homebrew installs sqlite3 as “keg-only” because otherwise it interferes with the system sqlite3. This means that libsqlite3.dylib ends up in /usr/local/opt/sqlite/lib, not in /usr/local/lib
So you can simply do this:
export DYLD_LIBRARY_PATH=/usr/local/opt/sqlite/lib:/usr/lib
Then you should be able to import sqlite3.

In my conda environment simply running conda install sqlite fixed it.

Related

Python pip not working with egg_info error [duplicate]

I'm trying to install some packages with pip.
But pip install unroll gives me
Command "python setup.py egg_info" failed with error code 1 in
C:\Users\MARKAN~1\AppData\Local\Temp\pip-build-wa7uco0k\unroll\
How can I solve this?
About the error code
According to the Python documentation:
This module makes available standard errno system symbols. The value of each symbol is the corresponding integer value. The names and descriptions are borrowed from linux/include/errno.h, which should be pretty all-inclusive.
Error code 1 is defined in errno.h and means Operation not permitted.
About your error
Your setuptools do not appear to be installed. Just follow the Installation Instructions from the PyPI website.
If it's already installed, try
pip install --upgrade setuptools
If it's already up to date, check that the module ez_setup is not missing. If it is, then
pip install ez_setup
Then try again
pip install unroll
If it's still not working, maybe pip didn't install/upgrade setup_tools properly so you might want to try
easy_install -U setuptools
And again
pip install unroll
Here's a little guide explaining a little bit how I usually install new packages on Python + Windows. It seems you're using Windows paths, so this answer will stick to that particular SO:
I never use a system-wide Python installation. I only use virtualenvs, and usually I try to have the latest version of 2.x & 3.x.
My first attempt is always doing pip install package_i_want in some of my Visual Studio command prompts. What Visual Studio command prompt? Well, ideally the Visual Studio which matches the one which was used to build Python. For instance, let's say your Python installation says Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)] on win32. The version of Visual Studio used to compile Python can be found here, so v1500 means I'd be using vs2008 x64 command prompt
If the previous step failed for some reason I just try using easy_install package_i_want
If the previous step failed for some reason I go to gohlke website and I check whether my package is available over there. If it's so, I'm lucky, I just download it into my virtualenv and then I just go to that location using a command prompt and I do pip install package_i_want.whl
If the previous step didn't succeed I'll just try to build the wheel myself and once it's generated I'll try to install it with pip install package_i_want.whl
Now, if we focus in your specific problem, where you're having a hard time installing the unroll package. It seems the fastest way to install it is doing something like this:
git clone https://github.com/Zulko/unroll
cd unroll && python setup.py bdist_wheel
Copy the generated unroll-0.1.0-py2-none-any.whl file from the created dist folder into your virtualenv.
pip install unroll-0.1.0-py2-none-any.whl
That way it will install without any problems. To check it really works, just login into the Python installation and try import unroll, it shouldn't complain.
One last note: This method works almost 99% of the time, and sometimes you'll find some pip packages which are specific to Unix or Mac OS X, in that case, when that happens I'm afraid the best way to get a Windows version is either posting some issues to the main developers or having some fun by yourself porting to Windows (typically a few hours if you're not lucky) :)
It was resolved after upgrading pip:
python -m pip install --upgrade pip
pip install "package-name"
I got stuck exactly with the same error with psycopg2. It looks like I skipped a few steps while installing Python and related packages.
sudo apt-get install python-dev libpq-dev
Go to your virtual env
pip install psycopg2
(In your case you need to replace psycopg2 with the package you have an issue with.)
It worked seamlessly.
I got this same error while installing mitmproxy using pip3. The below command fixed this:
pip3 install --upgrade setuptools
Download and install the Microsoft Visual C++ Compiler for Python 2.7 from https://www.microsoft.com/en-in/download/details.aspx?id=44266 - this package contains the compiler and set of system headers necessary for producing binary wheels for Python 2.7 packages.
Open a command prompt in elevated mode (run as administrator)
Firstly do pip install ez_setup
Then do pip install unroll (It will start installing numpy, music21, decorator, imageio, tqdm, moviepy, unroll) # Please be patient for music21 installation
Python 2.7.11 64 bit used
Other way:
sudo apt-get install python-psycopg2 python-mysqldb
I had the same issue when installing the "Twisted" library and solved it by running the following command on Ubuntu 16.04 (Xenial Xerus):
sudo apt-get install python-setuptools python-dev build-essential
It's a dependency issue.
I tried running the following commands helped me sorting out the dependencies, in my case the dependency was
grpcio
pip3 install --upgrade pip
python3 -m pip install --upgrade setuptools
pip3 install --no-cache-dir --force-reinstall -Iv grpcio==1.36.1
pip3 install pulsar-client==2.7.0
remember you must have python3 installed in your system.
First try:
pip install unroll
For sure not work :)
Then Try:
pip2 install unroll
Still get error Try:
pip3 install unroll
If pip3 Worked then suggest to change configuration to use pip3 as pip because you will get a lot of issues as the modern now is Python3 = pip3 if you execute a script files.
I had the same problem.
The problem was:
pyparsing 2.2 was already installed and my requirements.txt was trying to install pyparsing 2.0.1 which throw this error
Context: I was using virtualenv, and it seems the 2.2 came from my global OS Python site-packages, but even with --no-site-packages flag (now by default in last virtualenv) the 2.2 was still present. Surely because I installed Python from their website and it added Python libraries to my $PATH.
Maybe a pip install --ignore-installed would have worked.
Solution: as I needed to move forwards, I just removed the pyparsing==2.0.1 from my requirements.txt.
I ran into the same error code when trying to install a Python module with pip.
#Hackndo noted that the documentation indicate a security issue.
Based on that answer, my problem was solved by running the pip install command with sudo prefixed:
sudo pip install python-mpd2
For me this worked
python3 -m pip3 install -U pip
you can also try
python -m pip install -U pip
pip3 install --upgrade setuptools
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with -m pip instead of running pip directly.
Use python3 -m pip "command", eg:
python3 -m pip install --user pyqt5
I tried all of the above with no success. I then updated my Python version from 2.7.10 to 2.7.13, and it resolved the problems that I was experiencing.
That means some packages in pip are old or not correctly installed.
Try checking version and then upgrading pip.Use auto remove if that works.
If the pip command shows an error all the time for any command or it freezes, etc.
The best solution is to uninstall it or remove it completely.
Install a fresh pip and then update and upgrade your system.
I have given a solution to installing pip fresh here - python: can't open file get-pip.py error 2] no such file or directory
next installation helps me:
pip3 install cython
This worked for me:
sudo xcodebuild -license
Upgrading Python to version 3 fixed my problem. Nothing else did.
I downloaded the .whl file from http://www.lfd.uci.edu/~gohlke/pythonlibs/ and then did:
pip install scipy-0.19.1-cp27-cp27m-win32.whl
Note that the version you need to use (win32/win_amd-64) depends on the version of Python and not that of Windows.
I had this problem using virtualenvs (with pipenv) on my new development setup.
I could only solve it by upgrading the psycopg2 version from 2.6.2 to 2.7.3.
More information is at https://github.com/psycopg/psycopg2/issues/594
I faced the same problem with the same error message but on Ubuntu 16.04 LTS (Xenial Xerus) instead:
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-w71uo1rg/poster/
I tested all the solutions provided above and none of them worked for me. I read the full TraceBack and found out I had to create the virtual environment with Python version 2.7 instead (the default one uses Python 3.5 instead):
virtualenv --python=/usr/bin/python2.7 my_venv
Once I activated it, I run pip install unirest successfully.
try on linux:
sudo apt install python-pip python-bluez libbluetooth-dev libboost-python-dev libboost-thread-dev libglib2.0-dev bluez bluez-hcidump
Had the same problem on my Win10 PC with different packages and tried everything mentioned so far.
Finally solved it by disabling Comodo Auto-Containment.
Since nobody has mentioned it yet, I hope it helps someone.
I had the same problem and was able to fix by doing the following.
Windows Python needs Visual C++ libraries installed via the SDK to build code, such as via setuptools.extension.Extension or numpy.distutils.core.Extension. For example, building f2py modules in Windows with Python requires Visual C++ SDK as installed above. On Linux and Mac, the C++ libraries are installed with the compiler.
https://www.scivision.co/python-windows-visual-c++-14-required/
Following below command worked for me
[root#sandbox ~]# pip install google-api-python-client==1.6.4
Methods to solve setup.pu egg_info issue when updating setuptools or not other methods doesnot works.
If CONDA version of the library is available to install use conda instead of pip.
Clone the library repo and then try installation by pip install -e . or by python setup.py install
upgrading python's version did the work for me.
I have just encountered the same problem when trying to pip install -e . a new repo. I did not notice that the contents of setup.py haven't been saved properly and I was effectively running the command with an empty setup.py.
Hence you may experience the same error message if the setup.py of the target package is either empty or malformed.
I solved it on Centos 7 by using:
sudo yum install libcurl-devel

Can't install psycopg2 package through pip install on MacOS

I am working on a project for one of my lectures and I need to download the package psycopg2 in order to work with the postgresql database in use. Unfortunately, when I try to pip install psycopg2 the following error pops up:
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1
Why is this happening?
I fixed this by installing Command Line Tools
xcode-select --install
then installing openssl via Homebrew and manually linking my homebrew-installed openssl to pip:
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2
on macOS Sierra 10.12.1
Try installing it with:
pip install psycopg2-binary
brew install postgresql
If postgresql exist then run:
brew upgrade postgresql
sudo pip install psycopg2
In venv:
env LDFLAGS='-L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib' pip install psycopg2==2.8.4
If you need use only the command pip install psycopg2 export the path in macOSX:
export LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib"
I fixed it with:
brew reinstall openssl
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
Running into a similar problem using pipenv install psycopg2:
ld: library not found for -lssl', ' clang: error: linker command
failed with exit code 1 (use -v to see invocation)', " error:
command 'clang' failed with exit status 1", '
----------------------------------------',
I tried all of the recommendations above and none of them worked. I'd faced this problem in another virtualenv a few months ago and remembered reading something about this being a psycopg2 version issue. So I tried installing with 2.7.1 and 2.8.3 and both still failed. Somehow version 2.7.7 worked:
pipenv install psycopg2==2.7.7
Wish I had time to look into this further to understand what's really happening. But for now I don't and this worked.
For Mac OS 11.06 BigSur M1 ship, installing under a pyenv
brew reinstall libpq
brew reinstall openssl
Or install if its not already the case
brew info libpq
brew info openssl
Combine the 2 exported output logically, from
For compilers to find openssl#3 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/openssl#3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl#3/include"
...
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"
into
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib -L/opt/homebrew/opt/openssl#3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include -I/opt/homebrew/opt/openssl#3/include"
Then you can reinstall psycopg2, forcing the recompile, otherwise you will have this problem at runtime
pip uninstall psycopg2
pip install psycopg2 --no-cache-dir
Install/update Xcode developer tools
xcode-select --install
Query postgres path
find / -name pg_config 2>/dev/null
Install psycopg2, use the path you got in step 2. Mine was '/usr/local/Cellar/postgresql/9.5.0/bin/pg_config'
PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/ pip install psycopg2
Well, tons of different options didn't work for me, but finally!
First, try this:
brew reinstall openssl
At the end of the reinstallation process you will get basically the following lines within Caveats section, which I've put in the zshrc config file since I use macOS Big Sur (version 11.1) and zsh terminal.
Open zshrc or other config file specific to your system:
nano ~/.zshrc
Paste the following lines at the end (or change these commands accordingly to the Caveats you've got), just keep in mind that OpenSSL provided by Homebrew might conflict with the system's one (I've got a warning that macOS provides LibreSSL) but it worked well anyway:
export PATH="/opt/homebrew/opt/openssl#1.1/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/openssl#1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl#1.1/include"
Save the file and exit by pressing ^X and confirming with Yes.
Hopefully you can now install psycopg2:
pip3 install psycopg2-binary
I'm still not sure what was the root cause of the problem, it just appeared suddenly when I tried to reinstall pipenv for my project (maybe some recent system updates).
It looks like the openssl package is not installed. Try installing it and pip install again. I'm not a macos user, but I believe that brew simplifies package management on that platform.
You might also need to install the Python development and postgresql development packages.
This works for me (on a terminal):
find / -name pg_config 2>/dev/null
then add the path:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.5/bin/
os version: MacOs Sierra
I hope this helps.
Old, but for folks using pipenv, I resolved this issue by looking at my dependency graph pipenv graph --reverse and noticing it required psycopg2-binary==2.8.2. Running pipenv install psycopg2-binary resolved the issue.
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" \
brew upgrade openssl
Then I could install psycopg2
try this -->pip install psycopg2-binary
In case you don't want to faff around with brew, here's the "MacOS-only solution":
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
After this, pip will gladly install psycopg2 from the source. Works with "Mojave" (macOS 10.14).
The only thing that worked for me was:
pip install psycopg2==2.7.7
Try installing:
pip install psycopg2-binary
it works
I ran into the same error on macOS Big Sur (11.5.1) and Python 3.9.6.
It turned out, the psycopg2 (as well as pandas) versions I was using were too old. The error disappeared and pip install started working fine after upgrading the above packages.
Ref: https://github.com/Azure/azure-cli/issues/12854#issuecomment-619213863
In general, if the solutions suggested here by others don't help, try upgrading the troubling python package.
On my scenario, it was performed on:
ProductName: macOS
ProductVersion: 12.1
BuildVersion: 21C52
I proceed to install the following libraries by brewcommand:
brew install openssl libpq
Set specific variables:
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib -L/opt/homebrew/opt/openssl#3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include -I/opt/homebrew/opt/openssl#3/include"
Proceed to install the python package psycopg2-binary via pip3:
pip3 install psycopg2-binary
If you are wondering why it is necessary to set those specific variables, take a look in the docs of the package via brew info command:
brew info openssl
brew info libpq
When the outcome is displayed, look for this sentence:
...
For compilers to find libpq you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"
...
For compilers to find openssl#3 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/openssl#3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl#3/include"
...
Thats why, when pip is installing a package, needs a reference where to look for the libraries/compilers.
A perfect place to set this variables would be ~/.bashrc, for future installations regarding postgres and ssl:
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib -L/opt/homebrew/opt/openssl#3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include -I/opt/homebrew/opt/openssl#3/include"
As per current/todey condition -
only solution for this is -
uninstall your python 3.8 or 3.9 or so on
and install ptyhon 3.7.4

How to install psycopg2 with pg_config error?

I've tried to install psycopg2 (PostgreSQL Database adapater) from this site, but when I try to install after I cd into the package and write
python setup.py install
I get the following error:
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
I've also tried 'sudo pip install psycopg2' and I got the same message.
After reading through the docs, it asks to look at the setup.cfg file (which is below):
[build_ext]
define=
# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
# PSYCOPG_DEBUG can be added to enable verbose debug information
# "pg_config" is required to locate PostgreSQL headers and libraries needed to
# build psycopg2. If pg_config is not in the path or is installed under a
# different name uncomment the following option and set it to the pg_config
# full path.
#pg_config=
# Set to 1 to use Python datetime objects for default date/time representation.
use_pydatetime=1
# If the build system does not find the mx.DateTime headers, try
# uncommenting the following line and setting its value to the right path.
#mx_include_dir=
# For Windows only:
# Set to 1 if the PostgreSQL library was built with OpenSSL.
# Required to link in OpenSSL libraries and dependencies.
have_ssl=0
# Statically link against the postgresql client library.
#static_libpq=1
# Add here eventual extra libraries required to link the module.
#libraries=
However, I'm not sure if I'm suppose to edit this file, since the documentation states the following:
then take a look at the setup.cfg file.
Some of the options available in setup.cfg are also available as command line arguments of the build_ext sub-command. For instance you can specify an alternate pg_config version using:
$ python setup.py build_ext --pg-config /path/to/pg_config build
Use python setup.py build_ext --help to get a list of the options supported.
I've gotten the list of options supported but I'm not sure where to go from there
Debian/Ubuntu
Python 2
sudo apt install libpq-dev python-dev
Python 3
sudo apt install libpq-dev python3-dev
Additional
If none of the above solve your issue, try
sudo apt install build-essential
or
sudo apt install postgresql-server-dev-all
With pip
Install the psycopg2-binary PyPI package instead, it has Python wheels for Linux and Mac OS.
pip install psycopg2-binary
I was getting this issue because I hadn't yet installed PostgreSQL on my machine.
For Mac
brew install postgresql
For Mac without brew
python3 -m pip install psycopg2-binary
If you need to install without compiling:
pip install psycopg2-binary
https://www.psycopg.org/docs/install.html#binary-install-from-pypi
Note: The psycopg2-binary package is meant for beginners to start
playing with Python and PostgreSQL without the need to meet the build
requirements. If you are the maintainer of a publish package depending
on psycopg2 you shouldn’t use ‘psycopg2-binary’ as a module
dependency. For production use you are advised to use the source distribution.
If you are on Ubuntu or any other debian-based distro, try
sudo apt-get install python3-psycopg2
Otherwise, you need to find and install the Postgresql client packages for your distribution. psycopg2 installation from source
On Macbook with M1 i had to install postgresql
brew install postgresql
(If you don't have brew: https://brew.sh)
then run the install again
python3 -m pip install psycopg2-binary
Upgrading pip worked for me: pip install --upgrade pip
For OSX with Macports, you can install sudo port install py38-psycopg2 for Python 3.8. You can search for your version of Python with port search psycopg2. At the time of writing, the versions range from 2.7 to 3.9 all up to date with version 2.8.6 of psycopg2.
Note: This probably will not help in a venv.
Edit: So to find your pg_config for a venv, run the command port contents postgresql13 | grep pg_config where postgresql13 is the version of the package of postgresql installed from the above port command. You are looking for a path such as /opt/local/lib/postgresql13/bin/pg_config. Export that to your PATH variable with a command such as export PATH=/opt/local/lib/postgresql13/bin/:$PATH.
Refer to this answer for Homebrew.
On Fedora (tested and built from source on Fedora 35)
pg_config is present in libpq5-devel.
Now try these steps:
sudo dnf install libpq5-devel
python setup.py build
sudo python setup.py install
If you installed Postgres.app on macOS, add its bin directory to the PATH environment variable:
export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH
For mac os
install postgresql with : brew install postgresql
then
install psycopg2 with : pip install psycopg2
worked for me
Incase you are using PyCharm/Visual Code Studio terminal, don't do it. Use the default system terminal, as it ensures correct format of brew install is done.
On M1 pro with Intel chip:
brew install postgresql
pip install psycopg2
For me just this worked on Mac:
pip install --upgrade pip
pip install psycopg2-binary
For Mac
I had this issue too. I had followed a guide that had me run
brew install postgresql#15
This worked, but I kept getting the error
Error: pg_config executable not found.
So I then tried to brew install postgres without the #15 after it, and it worked! So, if you have installed postgresql#15 or #any-number, try to brew install without a number.
brew install postgresql
Your error is becouse didn't install the prerequisites on your machine or environment.
You can install prerequisites from here.
In this case probably your missing prerequisite is postgresql you can try below instructors.
For Mac
brew install postgresql
For Linux
sudo apt-get install postgresql
For people building postgres and psycopg2 from source like me, another solution is here:
sudo su
export PATH=/usr/local/pgsql/bin:$PATH #or path to your pg_config
Now setup.py from psycopg2 could find pg_config correctly.
python3 setup.py install
or if you just want to use pip3, pip3 install psycopg2 should work too.

ImportError: No module named 'MySQL'

I have downloaded the Connector/Python for MySQL successfully. I used the following code in Python's shell to test my connection:
import mysql.connector
I received the following error message:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
import mysql.connector
ImportError: No module named 'mysql'
I can't figure out why MySQL is not being recognized.
I was facing the similar issue. My env details -
Python 2.7.11
pip 9.0.1
CentOS release 5.11 (Final)
Error on python interpreter -
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>
Use pip to search the available module -
$ pip search mysql-connector | grep --color mysql-connector-python
mysql-connector-python-rf (2.2.2) - MySQL driver written in Python
mysql-connector-python (2.0.4) - MySQL driver written in Python
Install the mysql-connector-python-rf -
$ pip install mysql-connector-python-rf
Verify
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
Thanks =)
For python3 and later use the next command:
$ pip3 install mysql-connector-python-rf
Use
pip3 install mysql-connector
to install the python packaged (if you are using Python 3. For Python 2 you can use pip).
The silly mistake I had done was keeping mysql.py in same dir. Try renaming mysql.py to another name so python don't consider that as module.
May be simple install from cli?
pip3 install mysql-connector-python-rf
Package name differs from import library name
Or my universal variant in code:
import pip
pip.main(['install','mysql-connector-python-rf'])
For new version of pip:
from pip._internal import main
main(['install','mysql-connector-python-rf'])
It's better - install needed modules in running python installation (if many)
I tried all the answers but not worked for me.
It is a python version problem, in the end, I realized that python 3 scripts need explicit pip command for python 3, at least on ubuntu 18.
python3 -m pip install mysql-connector
Try that out bud
sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
gunzip mysql-connector-python-2.1.3.tar.gz
tar xf mysql-connector-python-2.1.3.tar
cd mysql-connector-python-2.1.3
sudo python3 setup.py install
You need to use anaconda to manage python environment dependencies. MySQL connector can be installed using conda installer
conda install -c anaconda mysql-connector-python
run
pip list
to see list of packages you have installed. If it has
mysql-connector-python then that is fine.
Remember not to name your python script file as mysql.py
just a note, I just installed mysql on an ubuntu 16.04 server. I tried different options in the following order:
using the package from repository sudo apt-get install python-mysqldb: Was installed correctly, but unfortunatelly python returned ImportError: No module named 'mysql'
using the ubuntu package from Mysql: wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.4-1ubuntu16.04_all.deb. Installed correctly with sudo dpckg -i package_name, but python returned the same error.
using tar.gz file, installing with python, worked ok.
wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.6.tar.gz (used 2.1.4 at that time)
tar -xf mysql-connector-python-2.1.6.tar.gz
cd mysql-connector-python-2.1.6/
sudo python3 setup.py install
Did not investigate why the first two failed, might try later.
Hope this helps someone.
sudo python3 -m pip install mysql-connector-python
This problem was a plague to me!!! The 100% solution is to forget using the mysql module: import mysql.connector, instead use pymysql via import pymysql. I installed it via the instructions: python3 -m pip install PyMySQL
made a change to the:
Import statement
The connector
The cursor
After that everything worked like a charm. Hope this helps!
I found that #gdxn96 solution worked for me, but with 1 change.
sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
tar -zxvf mysql-connector-python-2.1.3.tar
cd mysql-connector-python-2.1.3
sudo python3 setup.py install
I was facing the same issue on mac and linux both despite installing the connector using the command as mentioned in the other answers.
What worked for me the following commands.
sudo python3 -m ensurepip --default-pip
python3 -m pip --version
sudo python3 -m pip install mysql-connector-python
I don't know what difference do they make since I am not a python programmer but would like to know if someone can enlighten.
👇️ in a virtual environment or using Python 2
pip install mysql-connector-python
👇️ for python 3 (could also be pip3.10 depending on your version)
pip3 install mysql-connector-python
👇️ if you get permissions error
sudo pip3 install mysql-connector-python
pip install mysql-connector-python --user
👇️ if you don't have pip in your PATH environment variable
python -m pip install mysql-connector-python
👇️ for python 3 (could also be pip3.10 depending on your version)
python3 -m pip install mysql-connector-python
👇️ using py alias (Windows)
py -m pip install mysql-connector-python
👇️ for Anaconda
conda install -c anaconda mysql-connector-python
👇️ for Jupyter Notebook
!pip install mysql-connector-python
Source: https://bobbyhadz.com/blog/python-no-module-named-mysql
I have found another reason:
If your program file's path contains space or special characters, then you'd get this error.
#nembokid
Thanks, I wanted to build on this answer. If you're using anaconda or any other form of environment management, be sure to select the python from that environment.
sudo /home/joseph/anaconda3/envs/stocks/bin/python -m pip install mysql-connector-python
I had the same issue I resolved it using the following steps:
Step 1 - in terminal type echo %path% look for a file that's similar "C:\Users\AppData\Local\Programs\Python\Python39"
Step 2 - in vs code type "ctrl+shift+p" select python interpreter
Step 3 - make sure you select the interpreter with the correct path, you found yours when you used echo %path%
Now run the program !
You need to use one of the following commands. Which one depends on different-2 OS and software you have and use.
sudo easy_install mysql-python (mix os)
sudo pip install mysql-python (mix os)
sudo apt-get install python-mysqldb (Linux Ubuntu, ...)
cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
yum install MySQL-python (Linux Fedora, CentOS ...)
I just moved source folder connector from folder mysql to site-packages.
And run import connector
For 64-bit windows
install using wheel
pip install wheel download from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python
For python 3.x:
pip install mysqlclient-xxxxxxxxx-win_amd64.whl
For python 2.7:
pip install mysqlclient-xxxxxxxxx-win_amd64.whl
My project was using pipenv. The following command worked for me:
pipenv install mysql-connector-python
I did try re-install with apt and pip.
But the only thing that worked was install from source.
On Ubuntu 18.04 x64
Here is how I fixed the issue on my end.
Issue
I couldn't neither compile nor have the package available on VSCode.
Context
System: Ubuntu 18.04
Python version: 3.6.*
Installed Packages
mysql
mysql-connector-python
mysql-connector-python-rf
How I solved:
Nothing really worked until I see this article:
https://dev.mysql.com/doc/connector-python/en/connector-python-cext-development.html
On python console, just set
use_pure = True
With that, I could use python based connector instead.
import mysql.connector
However, if something goes wrong, the C Extension can be used through the _mysql_connector module.
References:
https://dev.mysql.com/doc/connector-python/en/connector-python-cext-module.html
https://www.reddit.com/r/learnpython/comments/a0q4bq/mysqlconnector_how_to_use_use_puretrue/
https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
https://overiq.com/mysql-connector-python-101/installing-mysql-connector-python/
in my case - for python 3.x works
pip3 install mysql-connector-python
I am facing the same issue, search a lot but didn't found any useful answer. Finally I found a mistake that first you should check your script name. Your script name shouldn't be mysql.py.

Installing python packages with multiple versions on OSX

I am attempting to install a package for python3.4 on Mac OSX 10.9.4. As you know, python ships with OSX, so when I installed python3.4 I was happy to find that it came with its own version of pip, that would install packages to it (installing pip on a mac with multiple versions of python will cause it to install on the system's python2.7.)
I had previously tried installing this package (https://pypi.python.org/pypi/chrome/0.0.1) with my first installation of pip (the one tied to python2.7) and found that it successfully installed on that version, but not on any others.
I ran an install with the new pip keyword for python3.4 (which when called by itself spits out the help page so i know it works) and it told me that the package was already installed and to try updating. The update revealed that I already had the most recent version. so I tried uninstalling it from just the python3.4 and reinstalling to no avail, and got the same results when uninstalling pip from python2.7 and reinstalling only on version 3.4.
I know that's a bit hard to follow but hopefully that makes sense.
I also reviewed the content here with no success.
RESOLVED:
while python did have a directory named the same as a directory it uses with packages, this was not the correct directory, for me it was in a subdirectory of library. while documentation said that referencing pip2 would cause the package to install on python3.4, this was false. however, referencing pip3.4 worked for me.
My suggestion is that you start using virtualenv.
Assuming you have 3.4 installed, then you should also have pyvenv. As for pip and 3.4, it should already be installed.
Using for example version 3.4 create your own virtual environment and activate it:
$ mkdir ~/venv
$ pyvenv-3.4 ~/venv/py34
$ source ~/venv/py34/bin/activate
$ deactive # does what is says...
$ source ~/venv/py34/bin/activate
$ pip install ... # whatever package you need
With version 2.7 first install virtualenv and then create your own virtual environment and activate it. Make sure that setuptools and pip are updated:
$ virtualenv-2.7 ~/venv/venv27
$ . ~/venv/venv27/bin/activate
$ pip install -U setuptools
$ pip install -U pip
$ pip install ... # whatever package you need

Categories