How to Install Python Packages? – Part I

Articles From: QuantInsti
Website: QuantInsti

The beauty of Python is that we have a collection of modules and packages which have been created for the purpose of making coding with Python easier. Also, 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.

Let us learn all about Python packages and their applications with this interesting guide that covers:

  • What are modules and packages?
  • The need to install Python packages
  • Importing Python packages
  • Installing Python packages
    • PyPI – Python package index
    • Bonus: dir() function
  • Resolution of frequent user queries
    • Cannot install iexfinance using conda install
    • Import get_data from iexfinance does not work
    • Dependency packages – scikit-learn

What are modules and packages?

When we write Python code lines for a particular task, for instance, trade order execution or for entering a trade signal (buy or sell), we use the Python environment such as the Python or IPython console.

In order to write a long program, we might consider a text editor to write the Python code lines. This is known as writing a script.

As a program gets longer, we may want to split it into several small files for easier maintenance. Also, we can find a way to copy and paste the lines of code each time we wish to use the same, without having to create the code lines every time we want the same program.

To support this, Python has a way to put a code definition in a file and use them in any Python script (code lines). Such a file is called a module. The definitions from a module can be imported into other modules or in the program that we code.

Packages are nothing but 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 the modules in such a way saves the author of different modules from having to worry about each other’s global variable names. Also, the use of dots in the module titles saves the author of multi-module packages from having to worry about each other’s module names.

The need to install Python packages

Python has certain in-built 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 hosted with ❤ by GitHub


ModuleNotFoundError Traceback (most recent call last)
Input In [47], in <module>
----> 1 import backtrader
ModuleNotFoundError: No module named 'backtrader'

The output above shows an error which is because ‘Backtrader’ is not a built-in Python package and we tried to import it without installing it first.

Hence, to install the Backtrader, you can do so by using the “pip” package manager, then open the command prompt (or terminal for Mac users) and type the below code:

pip install backtrader hosted with ❤ by GitHub

Now, you can import Backtrader with the following command:

import backtrader as bt hosted with ❤ by GitHub

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 will have access to some in-built packages such as PandasNumPy by default when you install Python.

You can import these packages in your code using the following syntax.

# Import a Python package
Import pandas hosted with ❤ by GitHub

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.



Top-level package
Initialize strats package
Sub-package for data
Equity module
Sub-package for strategies
RSI module
Sub-package for operations

When importing the package, Python searches through the directories in sys.path looking for the package subdirectory. The 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.strategies.statisticalarbitrage hosted with ❤ by GitHub

Above statements load the equity and statistical arbitrage modules from the data and strategies sub-packages respectively under the strats package.

Installing Python packages

But where can you find these packages and how to install them? That is what we will find in this section of the blog.

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 when you install Python in your system.

Installing pip

We can install a pip via the command line by using the curl command, which downloads the pip installation perl script.

curl -O hosted with ❤ by GitHub

Once it is downloaded, we need to execute it in the command prompt with the Python interpreter. This command works for all operating systems such as Windows, Mac, etc.

python hosted with ❤ by GitHub

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 the following command.

sudo python hosted with ❤ by GitHub

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` hosted with ❤ by GitHub

For example, to install the Backtrader package you have to replace the ‘package_name’ with ‘backtrader’.

# Install a Python package
!pip install backtrader hosted with ❤ by GitHub


Collecting backtrader
 Downloading backtrader- (410 kB)
 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 410.1/410.1 kB 2.9 MB/s eta 0:00:00a 0:00:01
Installing collected packages: backtrader
Successfully installed 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 out about package versions in the next section of the tutorial on ‘how to install Python packages’.

A 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.

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 version of the ‘backtrader’ package.

# Install a specific version of the package
!pip install backtrader== hosted with ❤ by GitHub


Collecting backtrader==
style="padding-left: 40px;">Using cached
Installing collected packages: backtrader
Found existing installation: backtrader
Uninstalling backtrader-
Successfully uninstalled backtrader-
Successfully installed backtrader-

Check for the version of the package

You can use the following syntax to check for the version of the package.

package_name.__version__ hosted with ❤ by GitHub

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_ hosted with ❤ by GitHub



Things to note

  1. Pip installs the latest version of the package by default.
  2. While installing the specific version pip replaces the existing version if there is any.
  3. You can use the above syntax for installing the packages through the IPython notebook.
  4. If you wish to install using the command prompt you can use the same syntax by just removing the exclamation mark.

For example,

IPython notebook:

!pip install package name hosted with ❤ by GitHub

Command prompt:

pip install package name hosted with ❤ by GitHub

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’.

Stay tuned for the second installment to learn about Bonus: dir()function.

Originally posted on QuantInsti blog.

Leave a Reply

Note that all comments are held for moderation before publishing.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.