This blog serves as a guide for users who wish to learn how they could 'Install Python Packages' and also addresses the queries related to it.
Python and 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 Pandas, NumPy by default when you installed Python.
Importing python packages
You can import these packages in your code using the following syntax.
What happens if the package is not installed?
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.
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? Here's 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.
Syntax to install a package
You can 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'.
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
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 184.108.40.206 version of the ‘backtrader’ package.
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.
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.
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