Create Python Environment for development

Virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

Ubuntu 18.04 LTS comes with default Python3. In order to install pip3 and virtual environment use the below command.

Install Components

sudo apt-get install python3 python3-pip python3-venv

The above command update the python3 to the latest version, along with many dependencies and build essential tool too. The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available

# python3 –version
Python 3.6.5

# apt-get install python3-pip python3-pip python3-venv
# python3 –version 
Python 3.6.7

# pip3 –version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Create Virtual Environment

The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. Decide a directory, where you want to place it.

krishna@training:~$ mkdir my_project
krishna@training:~$ cd my_project/
krishna@training:~/my_project$
krishna@training:~/my_project$ python3 -m venv my_projectenv
krishna@training:~/my_project$ ls -ltrh
total 4.0K
drwxrwxr-x 6 krishna krishna 4.0K Dec 13 14:48 my_projectenv

krishna@training:~/my_project$ source my_projectenv/bin/activate
(my_projectenv) krishna@training:~/my_project$ 
(my_projectenv) krishna@training:~/my_project$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
pkg-resources (0.0.0)
setuptools (39.0.1)
(my_projectenv) krishna@training:~/my_project$

(my_projectenv) krishna@training:~/my_project$ pip install flask uwsgi
Collecting flask
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
    100% |████████████████████████████████| 92kB 272kB/s 
Collecting uwsgi
  Cache entry deserialization failed, entry ignored
Collecting click>=5.1 (from flask)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
    100% |████████████████████████████████| 81kB 64kB/s 
Collecting Werkzeug>=0.14 (from flask)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
    100% |████████████████████████████████| 327kB 496kB/s 
Collecting Jinja2>=2.10 (from flask)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
    100% |████████████████████████████████| 133kB 303kB/s 
Collecting itsdangerous>=0.24 (from flask)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/08/04/f2191b50fb7f0712f03f064b71d8b4605190f2178ba02e975a87f7b89a0d/MarkupSafe-1.1.0-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: click, Werkzeug, MarkupSafe, Jinja2, itsdangerous, flask, uwsgi
Successfully installed Jinja2-2.10 MarkupSafe-1.1.0 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.1.0 uwsgi-2.0.17.1
(my_projectenv) krishna@training:~/my_project$ 
(my_projectenv) krishna@training:~/my_project$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
Click (7.0)
Flask (1.0.2)
itsdangerous (1.1.0)
Jinja2 (2.10)
MarkupSafe (1.1.0)
pip (9.0.1)
pkg-resources (0.0.0)
setuptools (39.0.1)
uWSGI (2.0.17.1)
Werkzeug (0.14.1)
(my_projectenv) krishna@training:~/my_project$ 
(my_projectenv) krishna@training:~/my_project$ pip freeze > requirements.txt
(my_projectenv) krishna@training:~/my_project$ 
(my_projectenv) krishna@training:~/my_project$ cat requirements.txt 
Click==7.0
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.0
pkg-resources==0.0.0
uWSGI==2.0.17.1
Werkzeug==0.14.1
(my_projectenv) krishna@training:~/my_project$ deactivate 
krishna@training:~/my_project$

Conclusion

Successfully, setup virtual environment. Enjoy!!!

Leave a Reply

Your email address will not be published. Required fields are marked *