Before we start with the tutorial on how to install Python packages, let us take a step back and understand the role packages play in the Python ecosystem. The beauty of Python is that we have a collection of modules and packages which have been created for a certain purpose and the fact that it is open-source makes it incredibly easy for one individual to build on top of another person’s work and create something useful. Thus, you can get a simple code for performing simple arithmetic operations or a group of code, called modules and packages, which can help perform data analysis, all on the internet.
We will cover the following concepts in this tutorial
- What are Modules and Packages?
- Importing Python packages
- PyPI - Python Package Index
- Syntax to install a package
- Bonus: dir() function
- Resolution of frequent user queries
What are Modules and Packages?
Consider writing code directly on the Python or IPython console. The definitions that we create(functions and variables) will be lost if we quit the console and enter it again. Therefore, in order to write a longer program, we might consider switching to a text editor to prepare an input for the interpreter and running it with that file as an input instead. This is known as writing a script.
As a program gets longer, we may want it to split it into several small files for easier maintenance. Also, we may want to use a handy function that we have written in several programs without copying its definition into each program. To support this, Python has a way to put a code definition in a file and use them in another script or directly in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or in the program that we code.
Packages can be considered as a collection of modules. It is a way of structuring Python’s module namespace by using "dotted module names". For example, the module name matplotlib.pyplot designates a submodule named pyplot in a package named matplotlib.
Packaging modules in such a way saves the author of different modules from having to worry
about each other’s global variable names and the use of dotted module names saves the author of multi-module packages from having to worry about each other’s module names.
Importing Python packages
As we know Python is an open-source project. The Python developers community make their codes available for others in the form of packages under the open source license.
You can import these packages in your code using the following syntax.
# Import a Python package Import pandas
Suppose we want to design a package (a collection of modules) for the uniform handling of various trading strategies and their data. There are many different data files based on data frequencies, so we may need to create and maintain a growing collection of modules for the conversion between the various data frequencies. Also, there are many different strategies and operations that we might need to perform. All of this put together means we would have to write a never-ending stream of modules to handle the combinatorics of data, strategies, and operations. Here’s a possible package structure to make our lives easier.
Initialize strats package
Sub-package for data
Sub-package for strategies
Sub-package for operations
When importing the package, Python searches through the directories in sys.path looking for the package subdirectory. The __init__.py file is required to make Python treat the directories as containing packages. If we are to use this package, we can do so in the following manner:
import strats.data.equity import strats.strategies.statisticalarbitrage
Above statements loads the equity and statisticalarbitrage modules from the data and strategies sub-packages respectively under the strats package. So far, we understood how to import the packages, but how to install Python packages? Is there even a need to install them? Let’s find out in the next section.
What happens if the package is not installed?
As we had mentioned previously, Python has certain built-in packages which are installed along with the installation of Python. But what about the packages that do not come along with Python installation. If you try to import such packages without installing them first you would get an error called 'ModuleNotFoundError'.
For example, Backtrader is a Python package used for live trading and backtesting trading strategies. You can see the error when we try to import it.
# import the package import backtrader
This is because ‘backtrader’ is not a built-in Python package and we tried to import it without installing it first. But where can you find these packages and how to install them? In the next section of the tutorial on how to install Python packages, we will find out how.
PyPI - Python Package Index
Most open source Python packages are made available through PyPI - Python Package Index. It is a repository of software for the Python programming language. You can find the packages developed and shared by the Python community here. You can also publish your package through PyPI.
To install the packages from PyPI you would need a package installer. The recommended package installer for PyPI is ‘pip’. Pip is installed along when you install Python in your system. You need not worry about downloading or installing pip exclusively. However, we will still cover the installation of pip in the next section of ‘how to install Python packages tutorial’.
We can install a pip via the command line by using the curl command, which downloads the pip installation perl script.
curl -O https://bootstrap.pypa.io/get-pip.py
Once it is downloaded, we need to execute it in the command prompt with the Python interpreter.
If the above command fails on a Mac and Linux distribution due to permission issues (most likely because Python does not have permission to update certain directories on the file system. These directories are read-only by default to ensure that random scripts cannot mess with important files and infect the system with viruses), we may need to run following command.
sudo python get-pip.py
Syntax to install a Python package
In this section of ‘how to install Python packages’, we will understand how to use the following syntax to install a package using ‘pip’.
`!pip install package_name`
For example, to install the Backtrader package you have to replace the 'package_name' with 'backtrader'.
# Install a Python package !pip install backtrader
After installation, you can see a success message in the last line. This means the package can now be imported and used in your code. There are a number of institutions along with individuals who use different versions of Python itself, so it goes without saying that there might be versions of packages too. Let’s find about package versions in the next section of the tutorial on ‘how to install Python packages’.
Version of the package
PyPI lets the developer submit any number of versions of the package. It holds a record for each combination of package name and version submitted in the repository.
The 'backtrader' package also has different versions available. You can check for them here.
Using a different version of the Python package
If you want to use a different version of the package, you can install that using the following command.
Let us install the 126.96.36.199 version of the ‘backtrader’ package.
# Install a specific version of the package !pip install backtrader==188.8.131.52
Check for the version of the package
You can use the following syntax to check for the version of the package.
But first, you need to import the package. You can check for the version of the 'backtrader' package as follows.
# Import the package Import backtrader # Check the version of the package backtrader._version_
Things to note
- Pip installs the latest version of the package by default.
- While installing the specific version pip replaces the existing version if there is any.
- You can use the above syntax for installing the packages through IPython notebook.
- If you wish to install using command prompt you can use the same syntax by just removing the exclamation mark.
- IPython notebook: `!pip install package_name`
- Command prompt: `pip install package_name`
I hope this will clarify any queries or doubts that you might have about installing Python packages. One of the popular ways to traverse through the Python codes and packages is the dir() function. Let’s learn more about what it does in the next section of the tutorial on ‘how to install Python packages’.
We can use the built-in function dir() to find which names a module defines. It returns a sorted list of strings.
In : import arithmetic In : dir(arithmetic) Out: ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'addition', 'division', 'factorial', 'multiply'] dir()function | 61
Here, we can see a sorted list of names within the module arithmetic. All other names that begin with an underscore are default Python attributes associated with the module (we did not define them).
Without arguments, dir() lists the names we have defined currently:
In : a = 1 In : b = 'string' In : import arithmetic In : dir() Out: ['__builtins__', 'a', 'arithmetic', 'b', 'exit', 'Quit']
Note that it lists all types of names: variables, modules, functions, etc. The dir() does not list the names of built-in functions and variables. They are defined in the standard module builtins. We can list them by passing builtins as an argument in the dir().
In : import builtins In : dir(builtins) Out: ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'SyntaxError', ... ]
Let’s take a breather here and understand what we have understood so far. We have understood what modules and packages are and also how to install Python packages. We have also gone through the dir() function. We could have ended the tutorial here and answer any issues you face in the comments but we thought why not pre-empt it and try to address some of the common issues ourselves.
Resolution of frequent user queries
As you begin to learn Python, you might encounter certain issues with respect to the platform and might feel that you are the only one here. Well, over the years we have seen queries from users which are common and most of them struggle to find answers to them. Thus, we'll try to address some of them in this section of the how to install Python packages tutorial.
The queries we will address are:
- Cannot install iexfinance using conda install
- Import get_data from iexfinance does not work
- Dependency packages - scikit-learn
Note: iexfinance errors are not available in the updated version of Anaconda
Query #1: Cannot install iexfinance using conda install
If you have installed Python using Anaconda, then you are familiar with the installation of Python packages using conda installer.
To install ‘iexfinance’ you would write `conda install iexfinance`
But this would throw an error as follows.
This is because the package is not available in Anaconda repository.
You can check for the same in the repository. It shows no results.
As mentioned earlier, You can search for the ‘iexfinance’ package in PyPI and check out the result as shown.
Thus, you can use the ‘iexfinance’ package using PyPI’.
You need to use ‘pip’ instead of ‘conda’ in your Anaconda prompt to install the packages from PyPI.
Write ‘pip install iexfinance’ in the Anaconda prompt to install the iexfinance package.
All right! Let’s move on to the next query in the tutorial on how to install Python packages.
Query #2: Import get_data from iexfinance does not work
You can see the success message in the last line along with the version of the package installed. Currently, the version - 0.4.0 of ‘iexfinance’ is installed.
Also, whenever the package is updated, a new version is released.
To check the version of ‘iexfinance’ package at any point of time, you can run the following command in the Anaconda prompt.
`python -c "import iexfinance; print(iexfinance.__version__)"`
You can update to the most recent version of the ‘iexfinance’ package by just running the pip install command again. Pip by default installs the latest version of ‘iexfinance’.
The command, as mentioned earlier, is `pip install iexfinance`.
If you are updating the package in your system then you should be aware of the changes made and you might want to change your codes also accordingly.
For the ‘iexfinance’ package, the changes made from the previous version to the latest version are documented here under the ‘Backwards Incompatible Changes’ section.
Great! Just one more remaining. Let’s move on to the next query in the tutorial on how to install Python packages
Query #3: Check for the dependency packages - scikit-learn
Let’s go back to this image once more.
You can see while installing ‘iexfinance’ pip checked for a lot of other Python packages such as requests, pandas etc.,
These are the dependency packages that are required to run ‘iexfinance’ smoothly.
For ‘scikit-learn’ package you can find the dependency packages in the PyPI project description.
You can also see the versions for the dependency packages.
Whenever you try to install or upgrade ‘scikit-learn’, make sure the scipy and numpy packages are also upgraded to their latest versions.
You can run `pip install scipy` and `pip install numpy` to upgrade these packages. And then install or upgrade ‘scikit-learn’ package by running `pip install scikit-learn`.
Note: Restart the Kernels
Do not forget to restart the kernels in Jupyter or Spyder before you start using ‘scikit-learn’ package in your code. Otherwise, you would face an error while importing the ‘scikit-learn’ package.
This happens because when you open the Jupyter or Spyder to code they create a Python environment based on the package versions existing at that point of time. So, whenever you install or upgrade a new package you need to restart the kernel too.
In summary, we've seen how to install Python packages and also addressed some of the most frequently asked queries about Python here in this tutorial on how to install Python packages. Hope this helps in creating a smooth journey as you explore Python!
There are many people who might be new to Python or programming in general or never created any trading strategy. The learning curve could be steep if you are a beginner in both these skills. However, you can build the required skills gradually and practice regularly on the hands-on learning exercises given in our course by enrolling here: Algorithmic Trading for Everyone
Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.