Installing pymssql on Windows server [duplicate] - python

This question already has answers here:
"ImportError: DLL load failed" when trying to import pymssql on Windows
(3 answers)
Closed 6 years ago.
I need to run Python with the pymssql library to query SQL server and I spent a day installing and compiling stuff on Windows.
Installation of pymssql was very painful, until I developed this procedure:
install python
update pip
download OpenSSL from http://www.npcglib.org/~stathis/blog/precompiled-openssl/
add bin folder to Path environment variable
download FreeTDS from https://github.com/ramiro/freetds/releases
add lib folder to Path environment variable
pip install pymssql
I even created the .exe package, so I won't have to install all this on live machine. It worked OK. Executable also worked on some other Windows machine.
But, when I tried it on the server it didn't work for some reason. The error didn't make sense and I started to cry:
WindowsError: [Error -2146893795] Provider DLL failed to initialize correctly
Could not help myself with links like this one:
"Windows Error: provider DLL failed to initialize correctly" on import of cgi module in frozen wxpython app
I have hundreds of tabs opened in my browser and I think I've searched the entire Internet.
I decided I'd try the Python and pymssql installation on the server, which I wanted to avoid, but let's go. And of course my frustration didn't end here. After successful installation I still can't load pymssql:
C:\Users\Me>C:\Python27\python.exe
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:40:30) [MSC v.1500 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> import sys
>>> import pymssql
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: %1 is not a valid Win32 application.
Can anyone point out where I went wrong?

When setting up pymssql version 2.1.2 and later on Windows, be careful to ensure that:
You download the builds of FreeTDS and OpenSSL that correspond to the Python version (e.g., 2.7, 3.4, 3.5, ...) that you are using.
You download the build of FreeTDS with the same "bitness" of Python that you are using (32-bit or 64-bit).
When adding folders to the Windows PATH, you must
i. specify the folders in which the required DLL files reside, and
ii. choose the OpenSSL folder that corresponds to the "bitness" of Python that you are using: bin for 32-bit, or bin64 for 64-bit.
For more information, see the related question:
"ImportError: DLL load failed" when trying to import pymssql on Windows

Related

Python - import requests ImportError: No module named requests

I have installed python 2.7 and also I have requests package installed. I am not sure of the reason, the site-packages are in a different location and my python 2.7 core files are in a different location
C:\Program Files\Python27
C:\Users\MyID\AppData\Roaming\Python\Python27\site-packages
I can see requests and the binaries in the path C:\Users\MyID\AppData\Roaming\Python\Python27\site-packages\requests. Yet when my job runs, it throws an error as shown below.
File "C:\Program Files (x86)\Jenkins\workspace\code\xxxx.py", line 36, in <module>
import requests
ImportError: No module named requests
I have only one version of python installed.
C:\Users\MyID\Desktop\xxx\Automation\xxxxxx\venv\Scripts>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
This is most probably the problem of virtual environment, virtual environment (venv) isolate your work environment in such a way that you can have different workspace for same version of python, that's why you are getting the error, I suspect that you installed request module in original python, but trying to run the script in virtual environment, hence it's giving error
Here, try these steps and tell the result
Open CMD
Write cd C:\Users\MyID\Desktop\xxx\Automation\xxxxxx\venv\Scripts , Press enter.
Write python -m pip install requests
Wait for it and tell the result, this must fix your problem
For reference on virtual environment and pip, look at this.

'import pyodbc' leads to 'No module named pyodbc '

I recently ran pip install pyodbc. Which says it installed pyodbc successfully on the cmd.
However when I do import pyodbc in IDLE I receive an error saying 'No module named pyodbc'.
It seems only two files have been installed when I ran pip install pyodbc.
The two files:
pyodbc-4.0.26.dist-info
pyodbc.cp36-win32.pyd
VERSION of Python:
Python 3.6.3
Complete Error Message:
Error Message
How do I resolve this issue? Any help would be much appreciated, I'm new to python and this is quite frustrating.
The files that installed are fine. *.pyd is compiled library (see https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll).
Issues that may cause this:
IDLE running main python installation and you installed the package in virtual environment
You have several installations of python e.g. python 3.x alongside python 2.x
I think first of all try to run all the steps from the same cmd:
PS C:\Windows\system32> pip install pyodbc
Collecting pyodbc
Using cached https://files.pythonhosted.org/packages/17/00/7115c072d4d01da4feee740cf5d964b4367ba0f9843d334d64ef77fd2baa/pyodbc-4.0.26-cp36-cp36m-win_amd64.whl
Installing collected packages: pyodbc
Successfully installed pyodbc-4.0.26
# now just run python interpreter
PS C:\Windows\system32> python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyodbc
# no error
This should work...
And then try to understand if you have several virtualenvs or python instalations.
You can check which python IDLE run, by right click on it's shortcut and "open file location" for example

openCV DLL load failed: %1

>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: %1 er ikke et gyldigt Win32-program.
>>>
I downloaded the newest version of openCV from https://sourceforge.net/projects/opencvlibrary/files/opencv-win/
Then copied the x64.pyd from folder C:\opencv\build\python\2.7 to C:\Anaconda3\Lib\site-packages but it still complaints.
Python 3.5.1 |Anaconda 4.0.0 (64-bit)[MSC v.1900 64 bit (AMD64)] on win32
I saw an answer from another post here: ImportError: DLL load failed: %1 is not a valid Win32 application I found 2 dll files in my C:\opencv\build\bin and sat that path to my system variables. But did not result in success.
The Python OpenCV DLL must be made for your version of Python and your system architecture.
You could try each of these steps:
Download OpenCV for your Python version (2/3)
Try replacing the x64 version with the x86 version, AFAIK if you have installed the more common Python 32bit on a x64 system, you'll still need the x86/32bit version of OpenCV
There are a lot of different binaries here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv. Try to get the one exactly matching your Python version and System architecture and install it via pip (cp35 means CPython version 3.5 etc.).
If you have the OpenCV .whl file matching your system configuration, do
pip install file.whl
At least the last one should definitively work!

How to Fix the Broken BSDDB Install in the Default Python Package on Mac OS X 10.5 Leopard?

Do the following on the default Python install on Mac OS X 10.5 (Leopard) w/ Developer Tools:
noel ~ : python
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bsddb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/bsddb/__init__.py", line 51, in <module>
import _bsddb
ImportError: No module named _bsddb
nice, huh? How do I fix this without giving up and installing/configuring/maintaining my own Python package as per TMNC's suggestion or using MacPorts etc?
Edit
I've gone around the problem by installing Python2.4 and BSDDB via MacPorts.
My question still stands: why is the default install broken and is it possible to fix it.
Follow the instructions at http://marc-abramowitz.com/archives/2007/11/28/hacking-os-xs-python-dbhash-and-bsddb-modules-to-work/ .
The patch did not work for me and I had to replace the bsddb folder in
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
with the bsddb3 in:
/usr/local/lib/python2.7/site-packages/bsddb3
Make sure you backup the bsddb folder just in case.
This pain persists on OSX 10.8. I could not install bsddb3 using macports py-bsddb3 into a virtualenv. What was very simple and did work is:
install db53 from macports
download and unpack bsddb3 source (https://pypi.python.org/pypi/bsddb3/6.1.0)
sudo python setup.py –berkeley-db-incdir=/opt/local/include/db53 –berkeley-db-libdir=/opt/local/lib/db53 install
A solution I found was to install ActivePython, dig into its library (located in /Library/Frameworks/Python.framework/Versions/....your version here..../lib) and copy and paste the _bsddb.so file into my OS X 10.6 native python 2.6 install directory (/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/).
That fixed the missing _bsddb issue leaving me with a working version of bsddb in my native python install. Then, I just uninstalled ActivePython (instructions here)
The error I had was "No module named _bsddb". It turn out I didn't need to upgrade my bsddb. I wasusing the .pkl file created in windows. After renaming the pkl file to get it out the way Mac OSX Python recreated a new .pkl.db file and now it works perfectly.

Cannot import SQLite with Python 2.6

I'm running Python 2.6 on Unix and when I run the interactive prompt (SQLite is supposed to be preinstalled) I get:
[root#idev htdocs]# python
Python 2.6 (r26:66714, Oct 23 2008, 16:25:34)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sqlite
>>>
How do I resolve this?
The error:
ImportError: No module named _sqlite3
means that SQLite 3 does not find the associated shared library. On Mac OS X it's _sqlite3.so and it should be the same on other Unix systems.
To resolve the error you have to locate the _sqlite3.so library on your computer and then check your PYTHONPATH for this directory location.
To print the Python search path enter the following in the Python shell:
import sys
print sys.path
If the directory containing your library is missing you can try adding it interactively with
sys.path.append('/your/dir/here')
and try
import sqlite3
again. If this works you have to add this directory permanently to your PYTHONPATH environment variable.
PS: If the library is missing you should (re-)install the module.
import sqlite3
sqlite3 - DB-API 2.0 interface for SQLite databases.
You are missing the .so (shared object) - probably an installation step. In my Linux python installation, _sqlite3 is at:
${somewhere}/lib/python2.6/lib-dynload/_sqlite3.so
Python 2.6 detects where the sqlite3 development headers are installed, and will silently skip building _sqlite3 if it is not available. If you are building from source, install sqlite3 including development headers. In my case, sudo yum install sqlite-devel sorted this out on a CentOS 4.7. Then, rebuild Python from source code.
Try this:
from pysqlite2 import dbapi2 as sqlite
On my system _sqlite3.so located at:
'/usr/lib/python2.6/lib-dynload/_sqlite3.so'
Check that the directory is in your sys.path:
>>> import sys; print(filter(lambda p: 'lib-dynload' in p, sys.path))
['/usr/lib/python2.6/lib-dynload']
Does that fix your problem?
Python 2.5.4 (r254:67916, May 31 2009, 16:56:01)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sqlite
>>> import sqlite3
>>>
The 2.5.5. Mac port of Python 2.5 now has this hint:
"py25-sqlite3 #2.5.4 (python, databases)
This is a stub. sqlite3 is now built with python25"
And so an upgrade of the python25 port to python25 #2.5.5_0 made the import work again.
Since sqlite3 is among the dependencies of python25,
it is built anew when upgrading python25.
Thus,
$ sudo port upgrade python25
does the trick on Mac OS X, ports collection.
I face the same problem.
Steps to solve.
Download latest sqlite3 from sqlite website. sqlite-autoconf*
Install that in 3 simple steps
./configure
make
make install
Rebuild the python
make
make install

Categories