Saturday, July 27, 2024
HomeRecommended Introducing 4 benefits of using Python virtual environments and how to...

[By type] Introducing 4 benefits of using Python virtual environments and how to use them!

Python is an open source programming language developed by a Dutch programmer.It has a wide range of uses, including web applications, AI development, and big data analysis.

Python is developed so that the code is easy to write and read, and the coding is the same no matter who writes it. Also, Python has many libraries, and you can easily create programs using them.

What is a virtual environment?

One of the features of Python is the ability to create virtual environments.A virtual environment means that you can build a Python environment like another machine in the same machine.

 

A virtual environment is a logically independent environment that can be run without package dependencies or compatibility.

 

For example, even if you can only run the matching version in the original environment root, you can run it regardless of the version in the virtual environment.

4 advantages of using a virtual environment in Python

Python is a programming language that is currently used in many fields such as web applications and AI development.

 

Using a virtual environment with Python has many advantages.Here, we will introduce four advantages of using a virtual environment in Python.

Advantage 1 of using a virtual environment: Use multiple versions of Python

One advantage of using virtual environments is that you can use multiple versions of Python.
Different versions of Python cannot be used together in a normal environment, but in a virtual environment even different versions can be used simultaneously.

 

If you have multiple versions installed, you can use different versions by specifying the version in the virtual environment.

Advantage 2 of using a virtual environment: Modules can be replaced or added

The advantage of using a virtual environment is that you can replace or add modules.
By using a virtual environment, you can add or replace modules using the pip command, etc., without affecting the Python environment used in the entire system.

 

The installation method differs depending on the module, but the pip command is available in many modules.

Advantage 3 of using a virtual environment: Different versions can be used within the same module

The advantage of using a virtual environment is that it is possible to use different versions within the same module.

 

A module is like a file, functions are stored in the file, and the functions in the module can be used by loading other modules.

 

Different modules are used depending on the purpose, but usually the same version is used. However, if you use a virtual environment, you can use different versions within the same module.

Advantage 4 of using a virtual environment: Easy to create and easy to delete

The advantage of using virtual environments is that they are easy to create and easy to delete.
In Python, when creating or deleting a virtual environment, you can execute it simply by entering a command.

As Python is frequently upgraded, using a virtual environment is suitable for avoiding troubles, but it is useless if it is troublesome to build and delete it. It is very important that virtual environments are easy to create and remove.

Three main Python virtual environments

I found that Python’s virtual environment has many advantages, but there are several types of software for creating virtual environments. If you are using a Python virtual environment for the first time, you may not know which software to use.

 

So, here are three Python virtual environments.The virtual environments introduced here are all the main virtual environments of Python, so please refer to them.

Virtual environment 1: venv

venv is a virtual environment program that comes standard with Python 3.4 and later. If you have Python, you can use it immediately, and you can feel the security of a genuine program.In terms of functionality, it is not much different from virtualenv, which will be introduced in the third section, so you can choose it based on ease of use.

 

However, unlike virtualenv, the command line is reset after starting the virtual environment, so you have to exit to see previous commands.

Virtual environment 2: conda

Conda is a virtual environment included in software called Anaconda, which is installed by many people who use Python for the first time.

conda is a virtual environment that is easy to manage and easy to use. Even if the OS is different, the usage is the same, and it is possible to download the related library.

 

In addition, since the Python version specification can be easily set, it is possible to immediately change to a different virtual environment.

Virtual environment 3: virtualenv

virtualenv is a 3rd party library for Python that needs to be installed if you want to use it.

 

After installation, if you want to create a virtual environment, you can create a virtual environment called “○○” simply by executing the command “virtualenv ○○”.

Also, virtualenv doesn’t reset the command line after the virtual environment starts, so you can easily see previous commands.

[By type] 4 ways to use virtual environments

So far, I have introduced three recommended virtual environments if you want to use a virtual environment with Python.

However, there are many people who do not know what kind of command to type when they actually want to create a virtual environment.

 

Here, we will explain how to create, use, delete, and terminate virtual environments in the three main virtual environments mentioned above.It’s a basic procedure for using a virtual environment, so let’s check it out.

[venv] How to use virtual environment 1: How to create

To create a virtual environment with venv, execute the command “python -m venv ○○” (where ○○ is the virtual environment name).The virtual environment program venv can be used immediately if it is a version of Python 3.4 or later.

 

First, check the version of Python, and if it is the appropriate version, create a virtual environment. Then the command execution will create a virtual environment file in the working directory.

[venv] How to use virtual environment 2: How to use

To use a virtual environment with venv, execute the “○○\Scripts\activate” (○○ is the name of the virtual environment) command.Executing this command refreshes the command screen in Windows.

 

If you start venv and check the library with the “pip list” command, you can confirm that the library is empty. You can also use the command to download the library with venv and execute the program.

[venv] How to use virtual environment 3: How to delete

To delete the virtual environment with venv, you can delete it with the command “○○ rm -rf” (○○ is the name of the virtual environment).When the virtual environment is deleted, the working folder has also disappeared, so the virtual environment is disabled.

 

However, when deleting a virtual environment with venv, first terminate the virtual environment with “deactivate” in order to prevent erroneous operations.

[venv] How to use virtual environment 4: Termination method

To terminate the virtual environment with venv, you can terminate it by executing the command “○○$deactivate”.

 

Let’s confirm that the prompt is gone after executing this command. Also, to exit the virtual environment with venv, you can also deactivate it by logging out from the terminal.

[conda] How to use virtual environment 1: How to create

To create a virtual environment with conda, execute the command “conda create -n ○○”.After executing the command, you will be asked “Are you sure you want to proceed?”, so enter the y key.

 

This will create your virtual environment.

[conda] How to use virtual environment 2: How to use

As a way to use the virtual environment with conda, execute the command “activate ○○”.Then, the command directory that was “C:” is changed to “○○C:”, indicating that the virtual environment ○○ is being used.

[conda] How to use virtual environment 3: How to delete

Sometimes you use a virtual environment with conda and then want to remove it permanently.

 

To remove a virtual environment with conda, enter and execute the command “conda remove -n ○○ –all” (where ○○ is the name of the virtual environment you created).

[conda] How to use the virtual environment 4: Exit method

To exit the virtual environment with conda, you can turn off the command screen itself, or enter the command “deactivate” to exit the virtual environment while leaving the command screen.

 

Also, if you want to use another virtual environment from the conda virtual environment, you can shift to a different virtual environment by entering the command “conda activate ○○”.

[virtualenv] Virtual environment 1: How to create

To create a virtual environment with virtualenv, check if there is a virtualenv on your PC, and if not download it.And to create a virtual environment, enter the command “virtualenv ○○”.

Then the virtual environment files are set in the current working directory. Since work directories are created in the virtual environment created with virtualenv, a virtual environment will be created for each work directory.

[virtualenv] Virtual environment 2: How to use

To use a virtual environment with virtualenv, enter the command “python XX” (XX is a group of files set in the working directory) and execute it.

Since a virtual environment must be created for each working directory, the working directory that can be used in the created virtual environment has already been set.

[virtualenv] Virtual environment 3: How to delete

To delete a virtual environment with virtualenv, simply delete the folder created directly under the project.

To delete a virtual environment with virtualenv, you can execute it with the “rm -rf XX/” (where XX is the virtual environment name) command.

[virtualenv] Virtual environment 4: Exit method

To terminate the virtual environment with virtualenv, you can terminate it by executing the command “deactive”.Alternatively, you can also close the command window.

Let’s master Python comfortably using a virtual environment

Python has the advantage of being able to run programs regardless of version by using a virtual environment. Many people feel that the more they use the Python virtual environment, the more convenient it becomes.

Find a suitable virtual environment for yourself by referring to the information introduced here, and use the virtual environment to comfortably use Python.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Most Popular

Recent Comments