...
- Log on to your SAIL desktop.
- Click on the Start menu (the little Windows icon on the bottom left) → Anaconda3 (64-bit) → Anaconda Prompt (Anaconda3). This will open a command line window with a line of text like:
- (base) C:\Users\<your username will be here>
- Activate your conda environment by typing the following in the same command line window:
conda activate P:\<your username here>\<name of your new environment>
- So, if my username is 'leal' and I created an environment called 'mynewenv' in a folder called 'conda-envs', I would use the command:
conda activate P:\leal\conda-envs\mynewenv
- You will know when the environment is activated because the window will show a line of text like:
- (P:\<your username>\<your environment name>) C:\Users\<your username>
- Outside your SAIL desktop, go to Google and search '<name of the package you want to install> anaconda'.
- For example, if I want to install the package 'recordlinkage', I would search on Google for 'recordlinkage anaconda'.
- Select the Google result from anaconda.org; this should take you directly to the Anaconda page for the package.
- On the page, there will be a command that tells you how to install it. Sticking with the recordlinkage example, the webpage shows me that the command to install is:
conda install -c conda-forge recordlinkage
- Go back to your SAIL desktop and type this installation command into your Anaconda Prompt window, hitting 'Enter'.
- Wait for Anaconda to ask you if you want to proceed - hit 'y' on your keyboard and then press 'Enter'.
- Your package is installed!
...
Connecting to DB2 from a Python version > 3.7
There is a known problem with the ibm_db package compiled for Python versions above 3.7, which means we need to do some extra things so we can connect to it from our notebooks.
As a prerequiste, your Anaconda environment will need sqlalchemy, ibm_db, and ibm_db_sa packages installed.
You can then run this script (replacing your username and password in the variables) to connect to db2.
Code Block | ||
---|---|---|
| ||
import os
import sys
rootpath = [x for x in sys.path if x.split(os.sep)[-1] == "site-packages"][0]
dllpath = f"{rootpath}\\clidriver\\bin"
os.add_dll_directory(dllpath)
import ibm_db
import ibm_db_dbi
import ibm_db_sa
import pandas as pd
from sqlalchemy import create_engine, text
uid = "YOUR USERNAME HERE"
pwd = "YOUR PASSWORD HERE"
database = "pr_sail"
hostname = "db2.database.ukserp.ac.uk"
port = "60070"
security = "ssl"
ssl_client_keystoredb = r"R:\UKSeRP\DB2_SSL\chi.kdb"
ssl_client_keystash = r"R:\UKSeRP\DB2_SSL\chi.sth"
conn_str = (
f"db2+ibm_db://{uid}:{pwd}@{hostname}:{port}/{database}"
f"?Security={security}"
f"&SSLClientKeystoredb={ssl_client_keystoredb}"
f"&SSLClientKeystash={ssl_client_keystash}"
)
engine = create_engine(conn_str)
sql = "SELECT * FROM syscat.tables LIMIT 1"
with engine.connect() as conn:
ResultProxy = conn.execute(text(sql))
results = ResultProxy.fetchall()
df = pd.DataFrame(results, columns=ResultProxy.keys())
print(df) |
The reason for this problem is explained in this GitHub issue: https://github.com/ibmdb/python-ibmdb/issues/887
...
FAQs
Can i install R packages from CRAN?
...