Gathering Financial Data Article by Quantinsti
Notes on the Python Environment (always in development)
Article on setting up your Python Environment by Real Python
My notes below. Last update: 6/7/2019
Warning to non-Windows users: Although outside links included below will usually discuss details relevant to other operating systems like Mac and Linux, the notes here will be most relevant for Windows users - it's the only operating system besides DOS that I've ever really used (apart from a relatively brief experience with an iPhone).
These notes are not really meant to be all- inclusive but intended for people with systems and interests similar to my own.
First I recommend installing Python by installing Anaconda rather than downloading Python from Python.org. This is because if you download Python first and Anaconda second, you will have two versions of Python in different locations and it will be confusing. Furthermore, Anaconda is designed specifically to help you manage your environment.
Learning the Basics
There are many courses (free and low cost) that will walk you through the 'basics' of Python (e.g., data types, control with loops, writing functions, etc.) My personal favorite is in MIT opencourseware. That is how I started learning Python, and the course recommends using Anaconda as an installation package - but also instructed me to first download Python - which led to some confusion as mentioned above. See also the Financial Data Professional Institute's list of approved prerequisite coursesin their study guide.
If you have trouble with a piece of (primarily pure) Python code, this is extremely useful: Python Tutor. It was recommended in the MIT (open) course and is amazing!
When you are ready to really create something...
Once you are starting to feel comfortable with Python, you will need to install some packages to reap the full benefits of Python. There are two basic methods to do so: 1) Anaconda and 2) pip install. As I mentioned above, I highly recommend using Anaconda to obtain your packages. In some cases there may be a package that is not available on Anaconda. In those rare cases, yo may have to use pip, so I've included notes on how to do that.
Go to www.anaconda.com/distribution/ and choose the correct version (e.g. Windows version) of Anaconda for the latest Python version. An .exe file will go to your default downloads folder. You can then double click that and install the program - it will take a little while. Anaconda Navigator will be automatically added to your start menu, along with two other programs: Spyder and Jupyter notebooks, and something called an Anaconda Prompt. These two additional programs provide you with software to write your Python programs in. They are called interpreters. Please read the user guide in your Anaconda program!! It will explain a lot!
Using a package in the Python interpreter
To use the numpy (or other) package within Python, you must import it after it is installed. Anaconda automatically installs many packages (like numpy, but we'll use it as an example anyhow). To know which packages are already available (versus those that need to be installed), type in the interpreter:
If the module is not listed, most often you will want to search for it within Anaconda Navigator and install it from there. Otherwise, you may need to use pip (see below). Once you verify the package is installed, use the import command. There are several options available with the import function. To know more about them, within the python interpreter, type:
The most common way to import the numpy package is to type in your interpreter (e.g., spyder)
import numpy as np
The "as np" part is standard method to save you from typing out numpy whenever you want to use a numpy function.
To use a numpy function you have to precede it by np. only if you specified "as np" in the import step. Otherwise, it must be preceded by numpy.. as in numpy.mean(x).
For example, from our list of numpy functions, let's choose mean. This is how you call the function when we include "numpy as np" in the import statement:
When you download Anaconda, you also receive the pip package. As mentioned above, you usually need only use this method if Anaconda doesn't have the package you are looking for. Yet, please also see "But What is Your End Goal?" below. Depending on your deployment goals, your may need to use pip and cannot use Anaconda. So while anaconda will serve most of your needs during much of your learning curve, if you go far enough you will likely need to use pip install at some point.
The general procedure is that you:
A. If you have Anaconda, you are going to use the Anaconda prompt in your start menu.
There are directions here: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html#installing-non-conda-packages
B. If you do not have Anaconda but downloaded Python separately,
1) go to https://pypi.org/ and download the desired package, and
2) go to Windows command line (you get to the command line by going to Start and typing in cmd) and at the Windows command line, type: pip install package name .
A few additional details follow. For all details go to https://packaging.python.org/tutorials/installing-packages/
In step 1) After you find the package you are looking for (read the associated directions - sometimes it’s better to install using anaconda) and if appropriate, download the file from pypi. You may see several filename options.
From my computer, I want to choose a filename that includes cp35, win and amd64 (I am running windows, and know I have a 64bit machine by going to Control Panel /System) This (relatively old) link is why I chose cp35: https://stackoverflow.com/questions/37023557/what-does-version-name-cp27-or-cp35-mean-in-python
So I chose numpy-1.16.3-cp35-cp35m-win_amd64.whl to install the package called numpy I wanted to start with.
Where do you want to download the file to?
Directories, paths and such can be a tricky issue.
Double clicking on the preferred file at pypi automatically downloads a file with .whl extension (a "wheel" type file) to my download folder. But double clicking on that file (as one might to install other .exe programs) doesn't go well. Windows doesn't know how to open it. Don't be worried! Go to your Windows directory and follow step B2!
At the Windows command line, to install, e.g., the package called "numpy," simply type:
pip install numpy
Test your installation
First, we may want to know what functions are in the module/package? For details, see http://xahlee.info/python/standard_modules.html,
but for now all we want to do is type into our Python interpreter:
This gives us a list of all of the functions that are in the numpy package. If we see this list, we can be reasonably sure that the installation was successful. But we can go further and actually use a function belonging to the recently installed module within Python, as shown in the screenshot shown earlier.
Here is an article on the subject ofPython Modules and Packages - An Introduction
But what is your End Goal?
This page [packaging.python.org] may not make sense to you early in your voyage into Python land, but it is important. Read it now and come back to it as many times as necessary before deploying you first project. Of course you can also do a conda installation and read about that in your conda / conda build documentation.
Some Useful or Interesting Python Links
Errors, like straws, upon the surface flow,
he who would search for pearls must dive below.
~ John Dryden
Pearl Quest, LLC. Financial data science consulting & Education services