Python and related software for your operating system: MacOS: Install Homebrew and Python, install the ODBC driver and SQLCMD, and then install the Python driver for SQL Server. See Steps 1.2, 1.3, and 2.1 in Create Python apps using SQL Server on macOS. For more information, see Install the Microsoft ODBC Driver on Linux and macOS. Official Microsoft GitHub Repository containing code samples for SQL Server - Microsoft/sql-server-samples. Sql-server-samples / samples / tutorials / python / pymssql / mac / sample_python_mac.py. Fetching contributors Cannot retrieve contributors at this time. Raw Blame History. 33 lines (24 sloc) 1.01 KB.
Summary I'm using a Mac on Yosemite Version 10.10.1 trying to connect to a MS SQL Server database. I searched and couldn't find an updated detailed answer so here's a writeup that is mostly from this amazing article. I'm adding it on stackoverflow in case the link dies.
The idea is that we'll have the following layers to setup/connect. Layers.
PART 1 - pyodbc. PART 2 - freeTDS (can check with tsql). PART 3 - unixODBC (can check with isql). PART 4 - MS SQL (can check with a regular python program) Steps. Install Homebrew from - this is a package manager for Mac OSX. The article shows how to use another package manager 'MacPorts'. For my instructions, they're with homebrew.
Basically homebrew has a folder 'cellar' that holds different versions of packages. Instead of modifying your normal files, it instead points to these homebrew packages. We need to install Pyodbc, but pyodbc uses iODBC drivers by default (which comes installed with mac), but many people have issues making it work. So, we're going to use an alternative called unixodbc, which we would install in the future. For now, we need to configure the pyodbc installation such that it works with unixodbc. Go to PyPi and download pyodbc tarball and uncompress it. Then change these lines in setup.py: elif sys.platform 'darwin': # OS/X now ships with iODBC.
Settings'libraries'.append('iodbc') to: elif sys.platform 'darwin': # OS/X now ships with iODBC. Settings'libraries'.append('odbc') and now run python setup.py install.
This makes our pyodbc installation use unixodbc drivers by default. Install FreeTDS with brew install freetds -with-unixodbc (FreeTDS is the driver that sits between the Mac ODBC and MS SQL Server, chart here shows which version of TDS you should be using based on your specific Microsoft Server version; e.g. Tds protocol 7.2 for Microsoft SQL Server 2008). Configure freetds.conf file (The file should be in '/usr/local/etc/freetds.conf', which for Homebrew is a link to say '/usr/local/Cellar/freetds/0.912/etc', but yours might be somewhere different depending on version).
I edited the global and added my database info to the end (for some reason 'tds version = 7.2' would throw an error, but still work, while 8.0 just works): global # TDS protocol version tds version = 8.0 MYSERVER host = MYSERVER port = 1433 tds version = 8.0. Verify FreeTDS installed correctly with: tsql -S myserver -U myuser -P mypassword (you should see a prompt like this if it worked) locale is 'enUS.UTF-8' locale charset is 'UTF-8' using default charset 'UTF-8' 1. Install unixODBC with brew install unixodbc. Setup your unixODBC config files, which includes odbcinst.ini (driver configuration), and odbc.ini (DSN configuration file).
By default, my files were in: /Library/ODBC (Note: NOT my user library aka /Users/williamliu/Library). Or they could also be in your homebrew installation directory /usr/local/Cellar/unixodbc//etc. Open up your ' odbcinst.ini' file and then add the following (Note: Different if you use MacPorts. For Homebrew, this file is a link to the homebrew version e.g.
Was really helpful to me. Here are some notes on a couple differences I experienced along the way, in case they help others:. The already had the change required, so all I had to do was download it and run python setup.py install. (Note: The version I had installed with pip was still using iodbc, so that didn't work. The Verify FreeTDS installed step didn't allow me to connect to the database because I don't have access to the master, and there is apparently no way to specify. This seems to be a well known issue. I wasted a bunch of time trying to solve it, but failed and in the end it didn't block other steps from working.
These instructions say to put the username and password in odbc.ini. Since we need to say the login credentials again when we log in, I tried removing the UserName and Password from odbc.ini, hoping they weren't really necessary. (I'd rather have my password written down in fewer places!) This worked fine. I had to add the host to the userid in isql MYSERVER [email protected] MYPASSWORD (and in the Python code). (I hoped that this would mean I don't need the host in freetds.conf, but alas, that has to stay.).
By Your Mac system likely already has Python installed on it. However, this installation is normally a few years old — or whatever the age of your system happens to be. You won’t be testing the limits of Python programming technology — just getting a great start using Python.
The Leopard version of OS X (10.5) uses a really old version of Python 2.5.1. This particular version lacks direct access to the IDLE application. This tells you more about how to overcome this particular issue. Currently, the newest version of OS X (Mavericks, or 10.9) comes with Python 2.7. Depending on how you use Python, you might want to update your installation at some point.
Part of this process involves installing the GNU Compiler Collection (GCC) tools so that Python has access to the low-level resources it needs. The following steps get you started with installing a new version of Python on your Mac OS X system. Navigate to the with your browser. You see information regarding the latest version of Python.
Click the appropriate link for your version of OS X:. Python 3.3.4 Mac OS X 64-bit/32-bit x86-64/i386 Installer for 32-bit or 64-bit versions on the Intel processor. Python 3.3.4 Mac OS X 32-bit i386/PPC Installer for 32-bit versions on the Power PC processor The Python disk image begins downloading. Be patient: The disk image requires several minutes to download. Most browsers provide a method for monitoring the download process so that you can easily see how long the download will take. When the download is complete, your Mac automatically opens the disk image for you.
The disk image actually looks like a folder. Inside this folder, you see a number of files, including python.mpkg. The python.mpkg file is the one that contains the Python application.
The text files contain information about the build, licensing, and any late-breaking notes. Double-click python.mpkg. You see a Welcome dialog box that tells you about this particular Python build. Click Continue three times. The installation program displays late-breaking notes about Python, licensing information (click Agree when asked about the licensing information), and, finally, a destination dialog box.
Select the Volume (hard drive or other media) that you want to use for installing Python and click Continue. The Installation Type dialog box appears. This dialog box performs two tasks:. Click Customize to change the feature set that is installed on your system. Click Change Install Location to modify the place where the installer places Python. Click Install.
The installer may request your administrator password. Type the administrator name and password, if required, into the dialog box and click OK. You see an Installing Python dialog box. The contents of this dialog box will change as the installation process proceeds so that you know what part of Python the installer is working with.
After the installation is completed, you see an Install Succeeded dialog box. Click Close. Python is ready to use. (You can close the disk image at this point and remove it from your system.).
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |