G

Environments

Using environments is a great way to work with different versions of Python and/or packages. It also provides an isolated environment to install everything so that if something goes wrong, it won’t affect the rest of the system. Python environments are particularly handy when you need different versions of packages installed across different projects. You can also use environments to see all the package dependencies.

G.1 Conda Environments

The Anaconda Python distribution comes with conda. The “Getting Started” guide is a useful resource in this case.1 If you installed Anaconda with Python 3 (Appendix B), this appendix will show you how to create a separate environment that has a different version of Python in it. If we run python in the command line, we will begin with Python 3.9. Your exact version will differ from that shown in this book.

1. https://conda.io/projects/conda/en/latest/user-guide/getting-started.html

% python
Python 3.9.12 (main, Jun  1 2022, 06:34:44)
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

To create a new environment we run the conda command from the command line. We use the create command within conda and specify a --name for the environment. Here we are naming our Python environment py38. By default, the system will create a Python 3.9 environment, so we have to specify our Python version with python=3.8.

# type this in the (bash) terminal, not in python
conda create -n py38 python=3.8

After running the command, you will see the following output.

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /Users/danielchen/anaconda3/envs/py38

  added / updated specs:
    - python=3.8


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2022.07.19 |       hca03da5_0         124 KB
    certifi-2022.6.15          |   py38hca03da5_0         153 KB
    libffi-3.4.2               |       hc377ac9_4         106 KB
    ncurses-6.3                |       h1a28f6b_3         866 KB
    openssl-1.1.1q             |       h1a28f6b_0         2.2 MB
    pip-22.1.2                 |   py38hca03da5_0         2.5 MB
    python-3.8.13              |       hbdb9e5c_0        10.6 MB
    setuptools-63.4.1          |   py38hca03da5_0         1.1 MB
    sqlite-3.39.2              |       h1058600_0         1.1 MB
    ------------------------------------------------------------
                                           Total:        18.6 MB

The following NEW packages will be INSTALLED:

  ca-certificates pkgs/main/osx-arm64::ca-certificates-2022.07.19-hca03da5_0
  certifi         pkgs/main/osx-arm64::certifi-2022.6.15-py38hca03da5_0
  libcxx           pkgs/main/osx-arm64::libcxx-12.0.0-hf6beb65_1
  libffi          pkgs/main/osx-arm64::libffi-3.4.2-hc377ac9_4
  ncurses         pkgs/main/osx-arm64::ncurses-6.3-h1a28f6b_3
  openssl         pkgs/main/osx-arm64::openssl-1.1.1q-h1a28f6b_0
  pip             pkgs/main/osx-arm64::pip-22.1.2-py38hca03da5_0
  python          pkgs/main/osx-arm64::python-3.8.13-hbdb9e5c_0
  readline        pkgs/main/osx-arm64::readline-8.1.2-h1a28f6b_1
  setuptools      pkgs/main/osx-arm64::setuptools-63.4.1-py38hca03da5_0
  sqlite          pkgs/main/osx-arm64::sqlite-3.39.2-h1058600_0
  tk              pkgs/main/osx-arm64::tk-8.6.12-hb8d0fd4_0
  wheel           pkgs/main/noarch::wheel-0.37.1-pyhd3eb1b0_0
  xz              pkgs/main/osx-arm64::xz-5.2.5-h1a28f6b_1
  zlib            pkgs/main/osx-arm64::zlib-1.2.12-h5a0b063_2


Proceed ([y]/n)? y

Downloading and Extracting Packages
certifi-2022.6.15    | 153 KB    | ########################## | 100%
python-3.8.13        | 10.6 MB   | ########################## | 100%
openssl-1.1.1q       | 2.2 MB    | ########################## | 100%
setuptools-63.4.1    | 1.1 MB    | ########################## | 100%
ca-certificates-2022 | 124 KB    | ########################## | 100%
pip-22.1.2           | 2.5 MB    | ########################## | 100%
sqlite-3.39.2        | 1.1 MB    | ########################## | 100%
ncurses-6.3          | 866 KB    | ########################## | 100%
libffi-3.4.2         | 106 KB    | ########################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate py38
#
# To deactivate an active environment, use
#
#     $ conda deactivate

The last few lines of the output tell you how you can use your newly created environment. If we run conda activate py38 from the command line now, our prompt will be prepended with our environment name. If we run python in the terminal to launch Python, you will see that a different version of Python is now being used.

% python

Python 3.8.13 (default, Mar 28 2022, 06:13:39)
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.

To delete an environment, navigate to your anaconda3 folder. A folder there called envs stores all your environments. In this example, if we delete the py38 folder within envs, it’s as if we never created our environment, and it will be removed.

Within a given environment, any package or library we install (Appendix H) within it will be specific to that particular environment. Thus, we can have not only different versions of Python between environments but also different versions of libraries. You can create a separate Python environment (p4e for “Pandas for Everyone”) for this book as well.}

conda create --name p4e python=3

You can install the libraries needed by following the instructions in Appendix H.

G.2 Pyenv + Pipenv

Calvin Hendryx-Parker gave a great talk at PyCon 2022 on Bootstrapping Your Local Python Environment that goes over the Pyenv setup with the pyenv-virtualenv plugin: https://www.youtube.com/watch?v=-YEUFGFHWgQ

The Hitchhiker’s Guide to Python and Real Python also have resources on using Pipenv for virtual environments:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.186.109