Close Navigation
Learn more about IBKR accounts
How to Install Python Packages? – Part I

How to Install Python Packages? – Part I

Posted April 21, 2023
Chainika Thakar
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

import_package.py hosted with ❤ by GitHub

Output:

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

Backtrader.py hosted with ❤ by GitHub

Now, you can import Backtrader with the following command:

import backtrader as bt

bt.py 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

Pandas.py 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.

strats/
__init__.py
data/
__init__.py
equity.py
currency.py
options.py

strategies/
__init__.py
rsi.py
macd.py
smalma.py
peratio.py
fundamentalindex.py
statisticalarbitrage.py
turtle.py

operations/
__init__.py
performanceanalytics.py
dataconversion.py
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 __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

Strats.py 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 https://bootstrap.pypa.io/get-pip.py

curl.py 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 get-pip.py

pip_command.py 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 get-pip.py

sudo_python.py 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`

pip_package.py 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

Install_backtrader.py hosted with ❤ by GitHub

Output:

Collecting backtrader
 Downloading backtrader-1.9.76.123-py2.py3-none-any.whl (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-1.9.76.123

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

# Install a specific version of the package
!pip install backtrader==1.9.68.122

pip_version.py hosted with ❤ by GitHub

Output:

Collecting backtrader==1.9.68.122
style="padding-left: 40px;">Using cached https://files.pythonhosted.org/packages/f5/6f/e51e5b5969ad1e8071rader-1.9.68.122-py2.py3-none-any.whl
Installing collected packages: backtrader
Found existing installation: backtrader 1.9.78.122
Uninstalling backtrader-1.9.70.122:
Successfully uninstalled backtrader-1.9.70.122
Successfully installed backtrader-1.9.68.122

Check for the version of the package

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

package_name.__version__

package_syntax.py 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_

Check_version.py hosted with ❤ by GitHub

Output:

'1.9.68.122'

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

IPython.py hosted with ❤ by GitHub

Command prompt:

pip install package name

Command_prompt.py 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.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation 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 its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.