Beginner’s Guide: Setting up the virtual environment in VS Code for different Python-based projects.
Hello Guys,
I know you
must be waiting for my next blog so the wait is over and I am back to continue
with my "Python Development in Visual Studio Code - Quick Tour" series. Today we are going to learn - how to
set or create the virtual environment for different projects. But before we
start its very necessary to know what is “Virtual Environment” and why a
programmer really need to create one. So, let’s get digging 🤔
As described on the official Microsoft
Documentation:
By default,
any Python interpreter that you've installed run in its own global environment,
which is not specific to any one project. For example, if you just run python
(Windows) or python3 (macOS/Linux) at a new command prompt, you're running in
that interpreter's global environment. Accordingly, any packages that you
install or uninstall affect the global environment and all programs that you
run within that context.
Note: The Python Extension version 2018.8.1
and later automatically updates environments.
Although
working in the global environment is an easy way to get started, that
environment will, over time, become cluttered with many different packages that
you've installed for different projects. Such clutter makes it difficult to
thoroughly test an application against a specific set of packages with known versions,
which is exactly the kind of environment you'd set up on a build server or web
server.
For this
reason, developers often create a virtual environment for a project. A virtual
environment is a subfolder in a project that contains a copy of a specific
interpreter. When you activate the virtual environment, any packages you
install are installed only in that environment's subfolder.
When you then run a
Python program within that environment, you know that it's running against only
those specific packages.
Let’s get started:
Prerequisites:
1.
Install a version of Python3:
As I am currently
using “Ubuntu 18.04.1 LTS”, it comes with Python3,
that’s why I don’t need to download it. So, make sure that your system has
python3 installed by running command “python3 --version” in the terminal.
In-case your system
does not have Python3 installed, you can download it from official site python.org. Currently, you can use the Download Python 3.7.2 button that
appears first on the page (or whatever is the latest version).
2. (Linux) The built-in Python 3
installation works well, but to install other Python packages you need pip.
pip is a package management system used to
install and manage software packages written in Python. Many packages
can be found in the default source for packages and their dependencies — Python
Package Index (PyPI). ... pip is a recursive acronym for "Pip
Installs Packages".
You can install pip
with following command:
$ sudo apt update
|
$ sudo apt install python3-pip
|
And once it’s
installed, verify it by running the command below:
$ pip3 --version
|
The version number
may vary but it looks something like this:
Note: Make sure that whenever you
start a python-based project you must have created your virtual environment
before installing any package respective to that project. This will help your
project packages to be refined within that virtual environment i.e. at one
place.
Creating Virtual Environment
To create a
virtual environment, in your project folder first you need to create your
project folder for example “myproject”.
Now run the following command in your terminal to create the virtual
environment in your project folder here its “myproject”.
$ sudo apt-get install python3-venv
|
$ python3 -m venv myenv
|
Where
"myenv" is the name of the environment folder.
or, by running VS Code Application and using the option File > Open Folder.
Now we are required to set Python Environment and if you remember we already did this step in my first blog. So we will follow the same steps but here it’s a slight change - we are going to select our newly created virtual environment “myenv” as our current python environment instead of selecting global environment.
In VS Code, open the Command Palette (Ctrl+Shift+P) and then select the Python: Select Interpreter command:
The command presents a list of available interpreters that VS Code can locate automatically. The newly created virtual environment will now be available in this list. From the list, select the virtual environment in your project folder that starts with ./myenv or .\myenv:
Run Terminal: Create New Integrated Terminal (Ctrl+Shift+`) from the Command Palette, which creates a terminal and automatically activates the virtual environment by running its activation script.
The selected
environment appears on the left side of the VS Code status bar, and notice the
"(venv)" indicator that tells you that you're using a virtual
environment:
So, at the
time of deployment of your application to other computers, you can create a
requirements.txt file with the command
$ pip3 freeze > requirements.txt
|
The
requirements file contains all the information and name of all the packages
you've installed in your virtual environment. With only this file, you or other
developers can restore those packages.
$ pip3 install -r requirements.txt
|
By using a requirements file, you need not commit the virtual environment itself to source control.
This how a
virtual environment is created and is used in VS Code. Well that’s all for now, I will be continuing with "How to start a project in Django in
VS Code” in my next blog " Beginner's Guide: Starting a Django Project in VS Code ". Till then, keep quenching your thirst for CODE...
Thanks for your valuable time. Have a nice day :-) 😄
Note: THE INFORMATION PROVIDED IN THIS POST IS "AS
IS" WITHOUT WARRANTY OF ANY KIND.
Helpful article.
ReplyDeleteThank you
Deletemade the process very simple for me, thank you
Delete