How to setup virtual environment with Python3

Please follow and like us:

Python3 is now becoming extremely popular over the traditional Python2.7. And the reasons are varied but many. For one, the support for data science libraries in v3 is far better and greater than version 2. But for the most important and critical reason is, the popular Python 2.7 is being officially retired by 2020. Which simply means that there will be no official support for it beyond that year. So it’s time to migrate your code to the latest edition. Here in this tutorial, we will show how to setup a virtual environment for Python3 for our development purposes.

In this tutorial, we will use Anaconda. It is an open source software, primarily designed to support scientific and analytic Python (and R) packages. Over time it has grown to support  more than 700 packages in both Python2.7 and Python3.x versions. So along with the usual scientific packages like pandas, numpy, sklearn, we have the traditional offerings like Flask, Pymongo libraries supported natively. Furthermore, with the use of pip within anaconda, we can download all that is available for Python out there.

From Anaconda website, download the installer for your platform. It is kinda heavy with nearly 500MB of installation package. But worth every byte. Follow the instructions online, which are quite intuitive, and install it.

Anaconda comes with a virtual environment called conda. It is very similar to virtualenv.

Managing Environments

To manage virtual environments, go to the folder you want to create the virtual environment and type the following.

conda create -n your_env_name

conda: this command creates a virtual environment in the folder that you are in.
is ofcourse the name of your environment. Say, myDataScience.

To install packages while creating the environment, use the following:

conda create -n your_env_name packages

Example for such a command could be:

conda create -n myDataScience pandas numpy Flask

The above command will create a virtual environment myDataScience and install three packages pandas, numpy and Flask in it.

You can also specify what version of Python to use:

conda create -n myDataScience python=3

Python=3 will make sure that conda virtual environment is python3 and all libraries will be python3 compatible, including pip.

To remove an enviroment use the following:

conda remove -n myDataScience –all

That’s it.

Activate Environment

To activate and deactive the virtual environment do the following:

source activate myDataScience

This will activate the myDataScience environment. And all work done within it will be within the virtual environment.

To deactivate/leave the virtual environment, simply type the following

source deactivate

Manage Packages

To install/upgrade packages type the following

conda install package_name

You can list as many packages in the line as possible. You can also specify specific versions like this:

conda install numpy=1.10

All packages installed will automatically look for dependencies and will install them along with the original package that you specify.

You can also install multiple packages simultaneously from a file

conda install –file requirements.txt

Here requirements.txt is the name of the file with all the module.

You can also as easily remove packages as you have installed them.

conda remove package_name

You can also upgrade packages installed by typing the following command

conda update –all

Sometimes, you will need to update conda itself to a newer version. Do that with the following command

conda update conda

If you want to search for a package within conda, type the following and it will find all flask named packages within conda

conda search flask

If you want to see what is installed within your virtual environment, simply type:

conda list

If something is not available in conda, then you can use the tradtional pip command from within the virtual environment

pip install newspaper3k

newspaper3k is a python package not natively supported in conda. But it can be easily installed within the virtual environment by using pip.

That’s it. Enjoy using Anaconda with python3 without any hassles.

Please follow and like us:

Techie by day, blogger by night. Love the outdoors, enjoy traveling and building new and interesting things. Follow me if you want to know something.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.