Key Word(s): Virtual environments, Python packages
Pair Programming Exercises¶
Week 8 (October 20th - October 26th): OPTIONAL¶
This week's excercises are optional. There are no deliverables. If you choose to participate, you should go through this notebook, work on the interactive examples, and try the labeled exercises on your own. The main ideas in this session are virtual environments and deployment of Python packages.
Practice with Packaging and Deployment¶
- Virtual environments
- Motivation
virtualenv
pipenv
conda
- Others: Why is pipenv the recommended packaging tool by the community and
PyPA
?
- Deployment
Note: No need to turn lecture exercises in today.
Package Requirements¶
Come up with a basic project directory structure (this one will be pretty bare bones). For example:
myproj\ myproj\ __init__.py module.py tests/ __init__.py README.md setup.py LICENSE
Note: I'm using the "Tests as part of application code" from the
pytest
documentation.
Virtual Environments¶
- You've got a package ready to go
- You probably (hopefully) developed it in the CS107 working directory
- What if you're developing many packages with different requirements?
- How do you deploy it?
Virtual Environments¶
- A virtual environment allows the developer to develop (and deploy) a package without having to worry about dependencies
- For example, your project may include several external dependencies
- You don't want to muck up or pollute your global environment with all these dependencies
- However, you need to install those dependencies to use the package!
Virtual Environments¶
- You may have different packages with different dependencies and you'd like to keep those environments separate while developing
- You may want to make it easy for a user to install dependencies so they can get up and running with your package right away
What are Virtual Environments?¶
A virtual environment is a directory with the following components:
site_packages/
directory where third-party libraries are installed.- Links [really symlinks] to the Python executables on your system.
- Some scripts that ensure that Python code uses the interpreter and site packages in the virtual environment.
virtualenv
¶
Getting Going with virtualenv
¶
- You probably already have
virtualenv
, but just in case typesudo easy_install virtualenv
- Now, navigate to the top level of your project directory and type
virtualenv env
- What do you see when you do
ls
?
- What do you see when you do
- Next type
source env/bin/activate
- What do you see?
Comments on virtualenv
¶
- You created and activated a virtual environment called
env
- Now everything you install will be in that virtual environment
- Within the virtual environment type
pip install sparsegrad
. (Seesparsegrad
)- Type
python
to start thepython
interpreter - In the
python
interpreter typeimport sparsegrad
- It works!
- Type
More comments on virtualenv
¶
- Now exit the
python
interpreter - And exit the virtual environment (Just type
deactivate
) - Open the
python
interpreter again- Notice that you're going to run
python
outside of the virtual environment
- Notice that you're going to run
- Type
import sparsegrad
- It's not there!
Summary so Far¶
Hopefully you have the basic idea now of the utility of virtual environments for development purposes.
They can also be useful for developers to share codes (deployment).
Suppose you have a package that you want to share with me.
You want to make it easy for me to install the dependencies, but otherwise you trust that I more or less know what I'm doing.
A Scenario¶
Here's a possible scenario:
- You send me the package (probably in an archive file like
.tar.gz
) and therequirements.txt
file - I create a directory called
myproj
and unpack the project into that directory - Then I do the following:
virtualenv env
source env/bin/activate
pip install -r requirements.txt
conda
¶
conda
environments¶
virtualenv
is great, but conda
environments are a bit more powerful.
pip
is a package manager for Python.virtualenv
is an environment manager for Python.conda
is a package and environment manager and is language agnostic
- Create an environment with the command
conda create --name env_name python
env_name
is the name of the environment.
- You can specify a different Python version with
conda create --name env_name python=3.7
- You can even install other packages:
conda create -n env_name python=3.7 numpy
- Note: You can also specify which versions of the packages you'd like to install.
Activating the environment¶
You can try
conda activate env_name
. This will work on many platforms.- It is possible that
conda
will complain that your shell hasn't been configured to useconda activate
. - You can run
conda init
, but this may modify yourbash_profile
to always use aconda base
environment.
- It is possible that
Alternatively, you may simply use
source activate env_name
.
- You can get out of the environment (deactivate it) by typing
conda deactivate
.
Where conda
environments live¶
- When working with
virtualenv
, we saw that our environment lives directly in the package directory. conda
environments live, by default, in/Users/user-name/anaconda3/envs
(or similar).- Note: You can override the default path by using the
--prefix
(or-p
) flag:conda create -p /path/to/env_name
.- This can complicate things. See the docs for more information.
- You can list your environments with
conda env list
.
Installing Packages in a conda
environment¶
- Inside the environment simply type
conda install package_name=version
. - List the installed packages in your environment with
conda list
. - You can install packages with
pip
as well.- You should try
conda install
first.
- You should try
Note: To remove a conda
environment, use conda remove --name env_name --all
.
Exercise (Part 1)¶
- Clone the course
playground
repo. - Go into
playground
. - Create a
conda
python
virtual environment- You can name it anything you like.
- Don't install
numpy
orpytest
for the moment.
- Activate the virtual environment.
- Try to run the tests with
pytest
.
Exercise (Part 2)¶
- Create a new
conda
python
virtual environment- Install
numpy
orpytest
when you build the environment.
- Install
- Activate the virtual environment.
- Try to run the tests with
pytest
.
Exercise (Part 3)¶
- Create a new
conda
python
virtual environment usingconda env create -n env_name -f environment.yml
- Activate the virtual environment.
- Try to run the tests with
pytest
.
Exercise Summary¶
- The virtual environment doesn't have the correct dependencies in general --- you need to customize this.
- In this exercise, the package came with an
environment.yml
file. - Using the
environment.yml
file, you can create a virtual environment with the exact same setup that the package developers use. - Note: If you want to generate the
environment.yml
file for your own packages, simply useconda env export -f environment.yml
.
Notes¶
- You may should probably remove the three environments that you just created.
- If you want to add the packages in an
environment.yml
file you can useconda env update -n conda-env -f environment.yml
.
Concluding Remarks on Virtual Environments¶
Advantages¶
- Pretty useful stuff if you're working with a fellow developer.
- To remove all the things that we don't want, just remove the virtual environment directories.
Disadvantages¶
- Not so great if we want to deploy our package to a wider audience
- The process can be streamlined a fair bit (e.g.
Pipenv
)
Deployment and Exercise 2¶
Now let's say you want to make your Complex
package more widely available.
You should follow the tutorial at Packaging Python Projects.
You can work with the tutorial practice package (rather than the Complex package).
Notes:
- Might need to use
conda install -c conda-forge twine
to gettwine
- You will need to verify your email address after you sign up for
TestPyPI
- Please use a
conda
environment rather thanvirtualenv
.
Further notes on Deployment¶
- Packaging in
python
can be a contentious topic (see the link at the beginning of this notebook) - For a pretty nice discussion on packaging in
python
refer to An Overview of Packaging for Python - For some more details on packaging in
python
see Packaging and distributing projects - Another packaging tutorial: How To Package Your Python Code