How to configure pymssql with SSL support on Ubuntu? - python

What were the steps required to configure (the now discontinued) pymssql with SSL support on Ubuntu so I can connect to a SQL Server instance that requires an encrypted connection (e.g., Azure)?

Ubuntu 16.04 LTS
(See this answer for Ubuntu 18.04 LTS.)
The following worked for me on a clean install of Xubuntu 16.04 LTS x64:
The first challenge is that the FreeTDS we get from the Ubuntu 16.04 repositories does not support SSL "out of the box", so we need to build our own. Start by installing python3-pip (which also installs build-essentials, g++, and a bunch of other stuff we'll need) and libssl-dev (the OpenSSL libraries required for building FreeTDS with SSL support)
sudo apt install python3-pip libssl-dev
Download the source code for FreeTDS by clicking the "Stable Release" link at freetds.org. Unpack the archive, switch to the directory you just created (e.g., freetds-1.00.104), and then do
./configure --with-openssl=/usr/include/openssl --enable-msdblib
make
sudo make install
Check the build with
tsql -C
and ensure that "TDS version: auto" and "OpenSSL: yes" are listed. Then use tsql to test a "raw" FreeTDS connection, e.g.,
tsql -H example.com -p 1433 -U youruserid -P yourpassword
Now to install pymssql. By default, recent versions ship as a pre-compiled "wheel" file that does not support encrypted connections so we need to install from the pymssql source. Starting with pymssql 2.1.4, the build process relies on Cython, so first do
pip3 install --user Cython
and then do
pip3 install --user --no-binary pymssql pymssql
When the build is complete, pymssql is installed.
But... it won't work (yet). When we try to do import pymssql in Python we get
ImportError: libsybdb.so.5: cannot open shared object file: No such file or directory
because apparently that file is in the "wrong" place. The fix (ref: here) is to create a symlink in the "right" place that points to the actual file
sudo ln -s /usr/local/lib/libsybdb.so.5 /usr/lib/libsybdb.so.5
sudo ldconfig
Now pymssql works with SSL connections.
For me, anyway.

Ubuntu 18.04 LTS
The Ubuntu 18.04 repositories will install a version of FreeTDS that supports GnuTLS so it is not absolutely necessary to build FreeTDS from source. However, we still need to build pymssql from source because simply doing the usual
pip3 install --user pymssql
will install a pre-compiled "wheel" that does not support secure connections. Instead, we need to do
sudo apt install python3-pip freetds-dev
pip3 install --user Cython
pip3 install --user --no-binary pymssql pymssql

For Ubuntu 16.04 it seems that at least the Docker containers have a FreeTDS version that already supports SSL.
Also, at least for Python 2.7, Cython is not needed:
https://github.com/tds-fdw/ci-setup/blob/master/ubuntu16.04/Dockerfile (lines 23-39)
But there's something to keep in mind!
The TDS version to connect to Azure must be forced to be at least 7.1 (or newer, depending on your needs: https://www.freetds.org/userguide/choosingtdsprotocol.htm)
Otherwise you will see the infamous:
[ERROR] (20017, 'DB-Lib error message 20017, severity 9:\nUnexpected
EOF from the server\nNet-Lib error during Operation now in progress
(115)\nDB-Lib error message 20002, severity 9:\nAdaptive Server
connection failed\n')
For some reason this was not needed for Ubuntu 14.04 and pymssql 2.1.3 without any extra configuration (https://github.com/tds-fdw/ci-setup/blob/master/ubuntu14.04/Dockerfile)
It can be done with either:
export TDSVER=7.1
Or, at the Python code, and at the connect function, adding the parameter:
tds_version='7.1'
With that, I am able to to use pymssql 2.1.4 to connect to Azure without issues.

you can use pymssql:2.2.0
first uninstall old pymssql:
sudo pip3 uninstall pymssql
second install new pymssql:
sudo pip3 install "pymssql==2.2.0"

Related

Problem installing MariaDB Python bindings on Raspberry Pi OS--how to get the latest Connector/C?

I am trying to use MariaDB with Python on a Raspberry Pi 4 with the 64 bit OS. After much trial and error I cannot solve the issue of a newer Connector/C version being required. The steps I took so far:
sudo apt update
sudo apt upgrade
sudo apt install mariadb-server
This installed MariaDB just fine and I can use it in a terminal. In order to install Python bindings I first installed
sudo apt install libmariadbclient-dev
but then when I try to install the Python module with
pip3 install mariadb
There is an error:
Collecting mariadb
Using cached https://files.pythonhosted.org/packages/71/a4/2d73d007571a5df9369eed9166150c7e067eb883cbd9ec3f97c7a48be660/mariadb-1.1.0.tar.gz
Complete output from command python setup.py egg_info:
MariaDB Connector/Python requires MariaDB Connector/C >= 3.2.4, found version 3.1.13
I wonder why version 3.2.4 is required in the first place. According to the documentation, it was just released 10 days ago: https://mariadb.com/kb/en/about-mariadb-connector-c/
There is no repository for Raspberry Pi OS. How can I install the newest Connector/C?
The answer for me was not to get the newest Connector/C, but to install an older version of the mariadb module. In my case going back to 1.0.7 was sufficient. It can be installed with
pip3 install -Iv mariadb==1.0.7
However, it seems like the newest pip version automatically falls back to the latest compatible module, so try upgrading pip first:
python3 -m pip install --user --upgrade pip
And then run the simpler command (without specific version number):
python3 -m pip install mariadb

ModuleNotFoundError : No module named 'psycopg2._psycopg'

I am using imports :-
import psycopg2
But while running the code in local, I am facing an issue of :-
from psycopg2._psycopg import (
ModuleNotFoundError: No module named 'psycopg2._psycopg'
I have installed psycopg2 using cmd [pip install psycopg2]
psycopg2 version 2.8.4
python version 3.7.3
python 3.7 32bit
How should I import the ._psycopg.py file in windows? or any other solution if possible?
I'm also facing the same problem. It took me 2 days to pulled out my hair until I found the solution.
What I've been tried
Here's what I have been doing to solve this issue BUT DIDN'T SOLVE THE PROBLEM. I hope it will help anybody else who has the same problem.
Install "psycopg2" and "psycopg2-binary"
pip install psycopg2
pip install psycopg2-binary
Install "libpq-dev"
sudo apt install libpq-dev
Install "python-psycopg2" and "python3-psycopg2"
sudo apt install python-psycopg2 python3-psycopg2
Install "python-dev" and "python3-dev" and "python3.9-dev"
sudo apt install python-dev python3-dev python3.9-dev
Install "python-setuptools"
sudo apt install python-setuptools
Unfortunately, none of my effort above solve my problem.
What's the solution
So, here's my solution that I've been try and IT WORKS
Install the latest mod_wsgi from Github.
You should install this latest 'mod_wsgi' in order to your wsgi able to use Psycopg2 library. Here's my step to do it :
Go to release page mod_wsgi on Github (https://github.com/GrahamDumpleton/mod_wsgi/releases)
Download the file from latest version. As I write this, the latest version is 4.7.1 (https://github.com/GrahamDumpleton/mod_wsgi/archive/4.7.1.tar.gz)
Follow the installation guide from official docs.
Unpacking source code
tar xvfz mod_wsgi-4.7.1.tar.gz
Configure source code
./configure
Build the source code and install
make
make install
Restart apache server
sudo service apache2 restart
This solution has works on my machine (Ubuntu 18.04 & Python 3.9 Venv).
Alternative solution
Here's some solution from others.
Saray Chak's solution

error to import MySQLdb in python

I have error to connect existing mysql database in python and i unable to do
pip install mysql command
import MySQLdb
db = MySQLdb.connect(host='10.10', port=3306, user='system', passwd='xyz' db='Reporting')
print(db)
I want to retrieve the data from mysql database in python.
Use pip install MySQL-python instead
You need to install the dependencies before installation with pip.
apt-get install python-dev libmysqlclient-dev
And than, the installation with pip like the answer of Zart should work.
pip install MySQL-python
The above answer is only for Linux System.
For Windows you can download the .whl file from here mysqlclient and install it. Please download the right version. win32 for 32 bit. amd64 for 64 bit. cp27 for python 2.7, cp34 for python 3.4, cp35 for python 3.5

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.

Adding database module

I am new to django
I would like to start a project but when i run it i get this error
Error loading MySQLdb module
How do i add the MYSQL module or any other module for that matter
Install it on your system, using either a native installer or package, via pip or easy_install, or by running setup.py in the tarball.
you should install a mysql client and a mysql python client for that client. So you should execute following commands(For Debian systems)
apt-get install mysql-client
apt-get install python-mysqldb

Categories