User Tools

Site Tools


labs:sqlalchemy

This is an old revision of the document!


Python and SQLAlchemy

In this lab you will get to know the basics of Python and some of its most useful libraries. After completing this lab, you will be awarded the title of SQL Alchemist.

Lab Setup

  1. Open terminal
  2. Execute
     ./opt/Uebungen/DatabaseLab/setup.sh 
  3. Execute
     source ~/.bashrc 
  4. Navigate to the folder for this lab:
     cd local-data/DatabaseLab 
  5. Create and activate virtual environment
  6. conda create --name labEnvironment python=3.6 sqlalchemy sqlite pandas numpy matplotlib
        source activate labEnvironment 
         

Getting Started

Ubuntu comes pre-installed with Python 2 and 3. It can also easily be installed using apt-get. However, these are system wide installations that are used by all Python programs. Since Python heavily relies on external libraries, and not all libraries are compatible with all versions of Python, it is often necessary to use different versions of Python for different projects. Thus, the use of Virtual Environments is highly encouraged. Every virtual environment has its own Python distributions and installed libraries. This way, different projects can be cleanly separated. There are several tools to create and manage virtual environments. In this lab we will use Anaconda.

Follow the link to figure out how to create and activate a new virtual environment. Specify the Python version to be 3.6 and install the following libraries using pip:

  • sqlalchemy
  • sqlite3
  • pandas
  • numpy
  • matplotlib

The command you need to use is:

 conda create --name labEnvironment python=3.6 sqlalchemy sqlite pandas numpy matplotlib

When programming with Python, one has the choice to use IDEs (Integrated Development Enviroments), text editors, an interactive python interpreter or more advanced tools like Jupyter Notebooks. In this lab, we will use the latter.

Let's verify that everything is working.

  1. Open a terminal window and activate your virtual environment
     source activate labEnvironment 
  2. List all installed packages and verify that sqlalchemy, sqlite3, pandas, numpy and matplotlib are instaleld
    conda list
  3. Check that the Python version of the virtual environment is indeed 3.6
    python -V
  4. Open the interactive Python interpreter
    ipython
  5. Print something
    print("Something")

If everything is set up correctly, you should see the print statement's output in the terminal.

  1. Open a text editor
  2. Add a print statement
    print("Something Else")
  3. Save the file as print.py
  4. Inside the terminal, navigate to the location where you saved print.py and execute the script
    python print.py

If everything is set up correctly, you should see the print statement's output in the terminal.

Now that all is set up, you are ready for the next step.

Jupyter Notebook

As mentioned earlier, there are many ways to code Python. In this lab you will be introduced to Jupyter Notebooks. A powerful and convenient method for programming with Python. All you need to do is open a terminal and navigate to the /home/lab/local-data/DatabaseLab/ directory where all the contents of this lab are stored. Then, issue following command:

jupyter notebook PythonSQL_Lab.ibynb

This should start the Jupyter Notebook server on Localhost and automatically open a browser window. If the browser is not opened automatically, open one yourself and type the following in the address bar:

http://localhost:8888/notebooks/PythonSQL_Lab.ipynb

For the remainder of this lab, you will be working in the Jupyter Notebook. Just follow the instructions there. Have fun!

labs/sqlalchemy.1495121326.txt.gz · Last modified: 2020/08/31 21:03 (external edit)