Background

When developing software with Python, a basic approach is to install Python on your machine, install all your required libraries via the terminal, write all your code in a single .py file or notebook, and run your Python program in the terminal.

This is a common approach for a lot of beginners and many people transitioning from working with Python for data analytics.

This works fine for simple Python scripting projects. But in complex software development projects, like building a Python library, an API, or software development kit, often you will be working with multiple files, multiple packages, and dependencies. As a result, you will need to isolate your Python development environment for that particular project.

Consider this scenario: you are working on app A, using your system installed Python and you pip install packageX version 1.0 to your global Python library. Then you switch to project B on your local machine, and you install the same packageX but version 2.0, which has some breaking changes between version 1.0 and 2.0.

When you go back to run your app A, you get all sorts of errors, and your app does not run. This is a scenario you can run into when building software with Python. And to get around this, we can use virtual environments.

This tutorial will cover everything you need to know about virtual environments and how to set one up with Virtualenv.

Tutorial

  1. Create a virtual environment(Named ‘ancillary’)

conda create –name ancillary python=3.7

conda create -n ancillary python=3.7

  1. Check the envs.
conda info --envs
  1. Activation
conda activate ancillary
  1. Check the packages(ipykernel)

pip list

or

python -m ipykernel –version

  1. install a new packages at this new environment

pip install ipykernel

  1. install jupyter menus(添加jupyter 内核)
python -m ipykernel install --user --name ancillary --display-name "Python(ancillary)"
jupyter lab

or

python -m ipykernel install –user –name kernelname

  1. Test kernel

查看内核

jupyter kernelspec list 删除内核 jupyter kernelspec remove kernelname

import sys
print(sys.executable)