Learn Python for Data Science (Course I)July 18, 2020 2020-08-26 9:26
Learn Python for Data Science (Course I)
- Welcome to Course I!
- Installing Python and Getting Started
- Data Types and Operations in Python
- Control Flow Tools in Python
- Errors and Exceptions in Python
- Classes and Objects in Python
- Standard Libraries in Python
- Virtual Environments and Packages in Python
- Working with Files in Python
- End of Course
Virtual Environments and Packages in Python
As discussed in the previous chapter, Python provides a decent set of standard libraries for a range of tasks. However, Python applications often require packages and modules that are not a part of the standard library. There are cases when applications need a specific version of a library since the application may be written using a specific version of the library. Hence, it is not possible for one Python installation to meet the requirements of every application.
The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. With this setup, you can use different virtual environments for different applications.
Creating Virtual Environments
To create a virtual environment, decide the directory where you want to place the virtual environment, navigate to that directory from the Command Prompt/Terminal and run the venv module:
python3 -m venv myenv
This creates the myenv directory if it does not exist, and also create directories inside it. These directories contain a copy of the Python interpreter, the standard library, and various supporting files. A common directory location for a virtual environment is .venv.
The directory tree of a virtual environment looks like the following:
project-directory/myvenv/ # Environment's root directory ├── bin │ ├── activate # Scripts to activate the virtual environment │ ├── activate.csh │ ├── activate.fish │ ├── easy_install │ ├── easy_install-3.7 │ ├── pip │ ├── pip3 │ ├── pip3.7 │ ├── python -> /usr/local/bin/python # Symlinks to system-wide │ └── python3 -> python3.8 # Python instances. ├── include ├── lib │ └── python3.8 │ └── site-packages # Stores local site packages └── pyvenv.cfg
Activating Virtual Environments
Once you’ve created a virtual environment, you need to activate it.
For Windows, run:
For Unix or MacOS, run:
After activation of a virtual environment, every python code you run will use the python interpreter of this environment.
1. Manually installing/uninstalling new packages
You can install, upgrade, and remove packages using a program called pip which is a package manager for Python. By default, pip installs packages from the Python Package Index, https://pypi.org.
The program manager pip has several subcommands: “search”, “install”, “uninstall”, “freeze”, etc. You can install the latest version of a package by specifying the name of the package.
pip install package_name
For example, package_name can be any package known by pypi such as numpy, pandas, matplotlib, etc. So, to install numpy, use the following command:
pip install numpy
Conversely, you can uninstall the package by using the uninstall command of pip.
pip uninstall package_name
2. Using the requirements.txt file
A requirements file is a text file that lists out all the external packages/modules and their version number that are installed in a particular virtual environment. The requirements file is extremely useful when we need to replicate a virtual environment on another computer so that an application runs perfectly on the other computer too.
To generate a requirements file for a virtual environment, run the following command after the environment is activated.
pip freeze > requirements.txt
After running this command, a new file named requirements.txt is created that lists all the modules installed in that environment. All the packages listed in the requirements.txt file can be easily installed in a new virtual environment by running the following command:
pip install -r requirements.txt