Python virtual environments
The recommended approach to using Python, in general, is a project-based one. This means that each project uses a separate Python version, along with the packages required and their mutual dependencies. This approach gives you the flexibility to switch between different Python versions and installed package versions. Not following this approach would mean that, every time you update a package or install a new one, its dependencies will be updated too, resulting in a different setup. This may cause problems, for example, code that won't run correctly because of changes under the hood, or packages that do not communicate correctly with each other. While this book focuses on Python 3, there won't be any need to switch to a different Python version, but maybe you can imagine using different versions of the same packages for different projects.
Before Anaconda, this project-based approach would require using virtualenv, a tool for creating isolated Python environments. This approach has gotten a lot easier with Anaconda, which offers the same approach but in a more simplified way. Both options are covered in detail as we proceed further.
Virtual environments using Anaconda
As stated before, Anaconda Navigator has a tab called Environments, that when clicked will display an overview of all local environments created by the user on a local file system. You can easily create, import, clone, or remove environments, specify the preferred Python version, and install packages by version number inside such an environment. Any new environment will automatically install a number of Python packages, such as pip. From there, you are free to install more packages. These environments are the exact same virtual environments that you would create by using the virtualenv tool. You can start working with them by opening a terminal or by running Python, which opens a terminal and runs python.exe.
Anaconda stores all environments in a separate root folder, keeping all your virtual environments in one place. Note that each environment in Anaconda Navigator is treated as a virtual environment, even the root environment.
Managing environments with conda
Both Anaconda and Miniconda offer the conda package manager, which can also be used to manage virtual environments. Open a terminal and use the following command to list all available environments on your system:
>> conda info -eUse the following command for creating a virtual environment based on Python version 2.7:
>> conda create -n python3packt python=2.7Activate the environment next as follows:
>> activate python3packtMultiple additional packages can now be installed with a single command:
>> conda install -n python3packt <package-name1> <package-name2>This command calls conda directly.
Deactivate the environment you've been working in as follows:
>> deactivateMore on managing environments with conda can be found at: https://conda.io/docs/user-guide/tasks/manage-environments.html
Virtual environments using virtualenv
If you don't want to use Anaconda, virtualenv needs to be installed first. Use the following command to install it locally:
>> pip install virtualenvNext, a virtual environment can be created by assigning with the virtualenv command followed by the name of the new environment, for example:
>> virtualenv python3packtNavigate to the directory with the same name:
>> cd python3packt Next, activate the virtual environment with the activate command:
>> activateYour virtual environment is now ready for use. Use pip install to install packages exclusively to this environment and use them in your code. Use the deactivate command to stop the virtual environment from working:
>> deactivateIf you have multiple Python versions installed, use the argument -p together with the desired Python version or path to the python.exe file of your choice, for example:
>> -p python2.7You can also do it as follows:
>> -p c:\python34\python.exeThis step follows creation of the virtual environment and precedes installation of the required packages. For more information on virtualenv, see: http://virtualenv.readthedocs.io/en/stable