How to Create a Python 3.6 Virtual Environment

Posted on

This is an article for creating a python virtual environment using python 3.6. There is a similar article with the title of ‘How to Create a Python Virtual Environment’ in this link. It is actually has the similar steps. But in order to describe it in detail, the following article will show how to do it. The following are the steps for achieving the purpose :

1. Check the availability of the python 3.6 in the operating system. Just type ‘python3.6’ as follows :

user@hostname:~$ python3.6
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 2hon59383]] on linux
Type "help", "copyright", "credits" or "license" for more information.

Apparently, the python 3.6 is available. If it is not, search the package responsible for installing python 3.6 and then install it. Do it as follows :

user@hostname:~$ apt search ^python3.6$
Sorting... Done
Full Text Search... Done
python3.6/bionic-updates,now 3.6.8-1~18.04.1 amd64 [installed,automatic]
  Interactive high-level object-oriented language (version 3.6)


Execute the following command to install python3.6 :

apt-get -y install python3.6

2. Finally, execute python 3.6 to create the python virtual environment. Do the following command ‘python3.6 -m virtualenv –path /usr/bin/python3.6 :

user@hostname:~/python$ python3.6 -m virtualenv -p /usr/bin/python3.6 env
Already using interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/user/python/env/bin/python3.6
Also creating executable in /home/user/python/env/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.

The above command is executing python3.6 using a module with the name of ‘virtualenv’. This module is a necessary module to create a python virtual environment. There is an additional parameter -p where it is useful to specify the path for the python for creating the python virtual environment. There is a message in the command output, ‘Already using interpreter /usr/bin/python3.6’ indicating that the command ‘python3.6’ actually already pointing to that path.

3. Last but not least, check if the folder with the name of ‘env’ exist after executing the previous step.

user@hostname:~$ cd /home/hamdi/python/
user@hostname:~/python$ ls -al 
user@hostname:~/python$ al en

4. Finally, execute the following command to activate the python virtual environment. Furthermore, type ‘python –version’ to check the python available in the python virtual environment.

user@hostname:~/python$ source env/in/activate
(env) user@hostname:~/python$ python --version
Python 3.6.8
(env) user@hostname:~$ 

Leave a Reply