Book Image

SciPy Recipes

By : V Kishore Ayyadevara, Ruben Oliva Ramos
Book Image

SciPy Recipes

By: V Kishore Ayyadevara, Ruben Oliva Ramos

Overview of this book

With the SciPy Stack, you get the power to effectively process, manipulate, and visualize your data using the popular Python language. Utilizing SciPy correctly can sometimes be a very tricky proposition. This book provides the right techniques so you can use SciPy to perform different data science tasks with ease. This book includes hands-on recipes for using the different components of the SciPy Stack such as NumPy, SciPy, matplotlib, and pandas, among others. You will use these libraries to solve real-world problems in linear algebra, numerical analysis, data visualization, and much more. The recipes included in the book will ensure you get a practical understanding not only of how a particular feature in SciPy Stack works, but also of its application to real-world problems. The independent nature of the recipes also ensure that you can pick up any one and learn about a particular feature of SciPy without reading through the other recipes, thus making the book a very handy and useful guide.
Table of Contents (11 chapters)

Creating virtual environments with venv

A virtual environment is a clone of a Python installation stored in a local folder. The Python executables and packages are not really copied, but symbolic links to the original files are created and environment variables are adjusted to set up a consistent Python filesystem. Once activated, a virtual environment will install packages in local directories without modifying the global Python installation.

A virtual environment is also the recommended setup for development in Python. With a virtual environment, it becomes easy to isolate the packages required to install the software under development.

If you are using Anaconda, do not create virtual environments using venv. Instead, use conda, as described in the previous section.

How to do it...

  1. To create the new environment in the myenv directory, use the following command in Linux and macOS:
python3 -m venv myenv
  1. In Windows, the command to be used is as follows:
python -m venv myenv

This creates an environment in the myenv subdirectory of the current directory. The -m switch is used because venv is a Python module that is being executed.

  1. To switch to the new environment on Linux and macOS, run the following in the command line:
source myenv/bin/activate
  1. On Windows, we use the following command:
myenv\bin\activate
  1. If we need to install a package in the new environment, we use pip or pip3 as usual. For example, to install a molecule package using pip3, we execute the following command:
pip3 install molecule
  1. Optionally, run the following command to create a requirements file:
pip3 freeze > requirements.txt

This will generate a report listing the packages required by the current environment and store the information in the requirements.txt file. This file can be distributed with the package being developed to facilitate installation by users that use pip.

  1. When done working in the environment, deactivate it by running the following command:
deactivate
  1. If we no longer need an environment, we can delete it as follows. First, activate the environment using the following commands:
    On Linux and macOS, use the following:
source myenv/bin/activate

On Windows, use the following:

myenv\bin\activate
  1. Create a list of all packages in the environment with the following command:
pip3 freeze > requirements.txt
  1. Remove all packages installed in the environment with the following command:
pip3 uninstall -y -r requirements.txt
  1. The -y switch prevents pip from asking for confirmation to uninstall each individual package. Next, deactivate the environment by running the following command:
deactivate
  1. Finally, remove the environment directory with the following commands:

On Linux and macOS use the following:

rm -rf myenv

On Windows use the following:

rmdir /s /q myenv
It is important to uninstall all packages using pip3 because some packages will copy files in directories outside of the environment directory. Typically, these directories are:
On Linux and Windows: The .local folder located in the user's home folder.
On macOS: /Users/username/Library/Python/x.x/bin, where username is the current user name and x.x denotes the version of Python the environment is based on.