python installation can't find psycopg2 ImportError - python

I'm calling on psycopg2 with
import psycopg2
I get the std error
ImportError: No module named psycopg2
I installed my copy with macports, so I'm curious why it wouldn't work because all of the dependencies should be downloaded as well.
I don't have any experience with Postgresql, nor this module, so I don't know what could be going wrong. Fact is, another project I'm trying to get built calls upon it, so if I could avoid using this I would. :)
I'm sure that postgresql is installed, but that has little to do with the fact that my installation can't find psycopg2. Any suggestinos would be appreciated.
$ which python
reveals
/Library/Frameworks/Python.framework/Versions/Current/bin/python
and
$ python --version
reveals
Python 2.7.3 -- EPD_free 7.3-2 (32-bit)
I don't necessarily want the version of EPD_free, but I had to install it for (somewhat) unrelated reasons.

MacPorts installs its own version of Python alongside Apple's version. You can manage the active version of Python (the one that gets run when you type in python at the command line or by /usr/bin/env) by using the port select command. See this question.

Related

Conda mysql package not recognized in any python interpreter

I've looked at other similar issues, but all my paths seem to be fine. I tried to install the python mysql library in conda so I could use it. To all metrics, it seems installed and recognized in the Pycharm interpreter, and when running conda list, I see mysql as an installed library, yet any time I try to import it, I get a ModuleNotFoundError. See below for a sample:
To isolate the issue, I chose to install another well known package that was not previously on my env, tenserflow, here's the results of that:
Evidently the issue is with the mysql library, since tensorflow installed and functioned as expected, meaning this isn't an issue with the interpreter, or an incorrect python path, or probably even conda.
I'm at a loss to what the issue is. I have mysql server installed, though that shouldn't matter, and I only have one environment. I'm trying to connect to a db, and have never used any mysql libraries on Python, so if there's others can be recommended, I have no preference, I just want my db!
Thanks in advance!
You have only installed the c library mysql, not the python library that you were looking for.
You need to do
conda install -c conda-forge mysql-connector-python
which will install the mysql python module that you are looking for

cx_Oracle on Python2 and Python3 (Windows)

I have been using Python 2.7 so far on my Windows 10 machine.
Now I need to migrate towards Python3.6, but I need to run both versions in parallel for quite a while until all old scripts have been converted to Python3. From the Python3 documentation, I gather that this should be possible.
I have installed both versions, which seems to have worked, and Hello World etc. works fine on both.
Now, one of the modules I routinely work with is cx_Oracle, and even though I have installed it for Python3, it does not seem to work. (I have installed both Python and cx_Oracle from .exe installers, both are 32 bit versions, just like the old Python2.7 (under which cx_Oracle works just fine).
import cx_Oracle
gives me
ImportError: DLL load failed: Procedure not found
I have adjusted PATH to contain both Python27 and Python3, and I have rebooted.
Can anyone tell me how to solve this? Does the Python3 version need its own Oracle Instant Client or something?
Edit:
I have installed cx_Oracle cx_Oracle-6.0b1-cp36-cp36m-win32 using pip, without errors, and rebooted again.
Now cx_Oracle works from within Eclipse PyDev but when I call the same script from IDLE, I get ''ModuleNotFoundError: No module named 'cx_Oracle''' ???

How do I change the kernel/python version for iPython?

I have installed iPython using pip in OS X 10.10, and it gave me the "ipython" and "ipython2" commands, which run great, but which use OS X's default python version 2.7.9. I downloaded and installed the latest release of Python3.4 and can load it with the command "python3," but cannot find a way to get iPython to use this version of python. The iPython Web site states the package can be used with python versions 3.3 and above, but I cannot find any instruction on how to change the default python version used.
So far I have found that the jupyter package for iPython has a kernel specification in /usr/local/share/jupyter/kernels/, which is just a folder called "python2" containing a json file that points to the system's python 2.7.6, but altering this to point to the new python3.4 installation does not work. My guess is this configuration is for the ipython notebook.
I've also tried the approach here: ipython reads wrong python version
In doing so I've duplicated the ipython2 command in /user/local/bin/ and edited it to use the python3 interpreter located at /Library/Frameworks/Python.framework/Versions/3.4/bin/python3, however, this gives me an error "ImportError: No module named 'IPython'," which suggests the python3 installation does not have ipython installed.
To tackle this, I've tried uninstalling ipython and reinstalling it using pip, but it just targets the system's Python 2.7 installation and does nothing for python3.
Does anyone know how to configure iPython to use a different python version, or even install a separate ipython installation for python3? Ultimately it would be nice to quickly switch back and forth depending on my needs.
I just found the answer. In essence, this stems from not understanding the python installation layout and how resources are separated between installed interpreters. It appears each python version will have its own repository of tools, and the current "pip" command I had installed on the system was mapped for use with python 2.7, so all libraries, tools, and other details it managed where available only to python 2.7. This included iPython.
I thought that installing python3 would make all these libraries available to the new interpreter by default, but it appears the system keeps them all separate (which makes sense). The main issue here was continuing to use "pip" which targeted the old installation, instead of the new "pip3" command included with python3. By using pip3 to reinstall iPython, I was able to get iPython3 installed properly.
Unfortunately this setup means needing to re-download other libraries, but that's not too difficult.
It might seem like a trivial issue in hindsight, but this had me completely stuck. I hope this helps someone else in this situation.

Python pysqlite2 dbapi2 problem

I'm having an issue with the line:
from pysqlite2 import dbapi2 as sqlite
The error i'm getting is:
ImportError: /usr/lib/python2.4/site-packages/pysqlite2/_sqlite.so: undefined symbol: sqlite3_enable_shared_cache
What can I do to solve this problem?
Thanks!
Sounds like _sqlite.so was compiled against a newer version of sqlite than you have installed. That function wasn't added to SQLite's API until version 3.5.0.
The easiest way around this problem is to get the AS package Python 2.6 or later from Activestate and install that. It comes with SQLITE in the standard library.
The AS package is a tarball and you install it in a user directory by running a shell script after unpacking the archive. This does not touch any of the Python bits installed with your system, and gives you a fully controlled Python environment that is easy to replicate on other systems regardless of the distro.
Python's packaging system doesn't interoperate well with Linux distro package systems, especially because the Linux distros can be considerably out of date.

Integrating MySQL with Python in Windows

I am finding it difficult to use MySQL with Python in my windows system.
I am currently using Python 2.6. I have tried to compile MySQL-python-1.2.3b1 (which is supposed to work for Python 2.6 ?) source code using the provided setup scripts. The setup script runs and it doesn't report any error but it doesn't generate _mysql module.
I have also tried setting up MySQL for Python 2.5 with out success. The problem with using 2.5 is that Python 2.5 is compiled with visual studio 2003 (I installed it using the provided binaries). I have visual studio 2005 on my windows system. Hence setuptools fails to generate _mysql module.
Any help ?
Download page for python-mysqldb. The page includes binaries for 32 and 64 bit versions of for Python 2.5, 2.6 and 2.7.
There's also discussion on getting rid of the deprecation warning.
UPDATE: This is an old answer. Currently, I would recommend using PyMySQL. It's pure python, so it supports all OSes equally, it's almost a drop-in replacement for mysqldb, and it also works with python 3. The best way to install it is using pip. You can install it from here (more instructions here), and then run:
pip install pymysql
This may read like your grandpa givin advice, but all answers here did not mention the best way: go nd install ActivePython instead of python.org windows binaries. I was really wondering for a long time why Python development on windows was such a pita - until I installed activestate python. I am not affiliated with them. It is just the plain truth. Write it on every wall: Python development on Windows = ActiveState!
you then just pypm install mysql-python and everything works smoothly. no compile orgy. no strange errors. no terror. Just start coding and doing real work after five minutes.
This is the only way to go on windows. Really.
As Python newbie learning the Python ecosystem I've just completed this.
Install setuptools instructions
Install MySQL 5.1. Download the 97.6MB MSI from here You can't use the essentials version because it doesnt contain the C libraries.
Be sure to select a custom install, and mark the development tools / libraries for installation as that is not done by default. This is needed to get the C header files.
You can verify you have done this correctly by looking in your install directory for a folder named "include". E.G C:\Program Files\MySQL\MySQL Server 5.1\include. It should have a whole bunch of .h files.
Install Microsoft Visual Studio C++ Express 2008 from here This is needed to get a C compiler.
Open up a command line as administrator (right click on the Cmd shortcut and then "run as administrator". Be sure to open a fresh window after you have installed those things or your path won't be updated and the install will still fail.
From the command prompt:
easy_install -b C:\temp\sometempdir mysql-python
That will fail - which is OK.
Now open site.cfg in your temp directory C:\temp\sometempdir and edit the "registry_key" setting to:
registry_key = SOFTWARE\MySQL AB\MySQL Server 5.1
now CD into your temp dir and:
python setup.py clean
python setup.py install
You should be ready to rock!
Here is a super simple script to start off learning the Python DB API for you - if you need it.
I found a location were one person had successfully built mysql for python2.6, sharing the link, http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
...you might see a warning while import MySQLdb which is fine and that won’t hurt anything,
C:\Python26\lib\site-packages\MySQLdb__init__.py:34: DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet
What about pymysql? It's pure Python, and I've used it on Windows with considerable success, bypassing the difficulties of compiling and installing mysql-python.
You're not the only person having problems with Python 2.6 and MySQL (http://blog.contriving.net/2009/03/04/using-python-26-mysql-on-windows-is-nearly-impossible/). Here's an explanation how it should run under Python 2.5 http://i.justrealized.com/2008/04/08/how-to-install-python-and-django-in-windows-vista/
Good luck
The precompiled binaries on http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python is just worked for me.
Open MySQL_python-1.2.5-cp27-none-win_amd64.whl file with zip
extractor program.
Copy the contents to
C:\Python27\Lib\site-packages\
On Python 3.4 I've installed mysqlclient from http://www.lfd.uci.edu/~gohlke/pythonlibs/ with pip install mysqlclient and it's working.
You can try to use myPySQL. It's really easy to use; no compilation for windows, and even if you need to compile it for any reason, you only need Python and Visual C installed (not mysql).
http://code.google.com/p/mypysql/
Good luck
There are Windows binaries for MySQL-Python (2.4 & 2.5) available on Sourceforge. Have you tried those?
Because I am running python in a (pylons/pyramid) virtualenv, I could not run the binary installers (helpfully) linked to previously.
I had problems following the steps with Willie's answer, but I determined that the problem is (probably) that I am running windows 7 x64 install, which puts the registry key for mysql in a slightly different location, specifically in my case (note: I am running version 5.5) in: "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MySQL AB\MySQL Server 5.5".
HOWEVER, "HKEY_LOCAL_MACHINE\" cannot be included in the path or it will fail.
Also, I had to do a restart between steps 3 and 4.
After working through all of this, IMO it would have been smarter to run the entire python dev environment from cygwin.
If you are looking for Python 3.2 this seems the best solution I found so far
for Python 2.4 - 3.2 PyMySQL
for Python 2.3 - 2.6 MySQL for Python
Source: http://wiki.python.org/moin/MySQL
You might want to also consider making use of Cygwin, it has mysql python libraries in the repository.
You can also use pyodbc with the MySQL Connector/ODBC to use MySQL on Windows. Unixodbc is also available to make the code compatible on Linux. Pyodbc uses the standard Python DB API 2.0 so if you stick with that switching between MySQL/PostgreSQL/SQLite/ODBC/JDBC drivers etc. should be relatively painless.
upvoted itsadok's answer because it led me to the installation for python 2.7 as well, which is located here: http://www.codegood.com/archives/129
Got sick of the installation troubles with MySQLdb and tried pymysql instead.
Easy setup;
git clone https://github.com/petehunt/PyMySQL.git
python setup.py install
And APIs are pretty much the same.

Categories