Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SciPy Recipes

You're reading from   SciPy Recipes A cookbook with over 110 proven recipes for performing mathematical and scientific computations

Arrow left icon
Product type Paperback
Published in Dec 2017
Publisher Packt
ISBN-13 9781788291460
Length 386 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
V Kishore Ayyadevara V Kishore Ayyadevara
Author Profile Icon V Kishore Ayyadevara
V Kishore Ayyadevara
Ruben Oliva Ramos Ruben Oliva Ramos
Author Profile Icon Ruben Oliva Ramos
Ruben Oliva Ramos
Luiz Felipe Martins Luiz Felipe Martins
Author Profile Icon Luiz Felipe Martins
Luiz Felipe Martins
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Getting to Know the Tools FREE CHAPTER 2. Getting Started with NumPy 3. Using Matplotlib to Create Graphs 4. Data Wrangling with pandas 5. Matrices and Linear Algebra 6. Solving Equations and Optimization 7. Constants and Special Functions 8. Calculus, Interpolation, and Differential Equations 9. Statistics and Probability 10. Advanced Computations with SciPy

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.
You have been reading a chapter from
SciPy Recipes
Published in: Dec 2017
Publisher: Packt
ISBN-13: 9781788291460
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image