Python development environment (Virtualenv)


Once you’ve finished installing Pycharm, you should now set up a virtual environment for your development.

A virtual environment is a clone of the Python Interpreter installed on your system so that it can be used only for desired projects.

 

For example:

Let’s say I have a PC and I need to run both Python A project and B project at the same time.

And if A project is developed as Python2.7 and B is 3.6, you should install all 2.7 and 3.6 versions of Python on your PC.

If you do not specify a virtual environment in this state, it will be very uncomfortable.

If you have two Python installations that normally run Interpreter by typing python on CMD

You have to go into that path and run python, or else you have to catch the environment variable differently.

 

Even if A and B are developed with the same Python version, the problem still exists.

Many libraries are used in Python development, which can vary depending on the nature of the project.

In other words, project A requires the C, D, and E libraries, and B may require F, G, and H libraries.

If you do not apply the virtual environment, the C, D, E, F, G, and H libraries apply to both A and B.

This may lead to conflicts between libraries or to unexpected results.

 

For this reason, we recommend using a virtual environment to ensure dependency between projects.

You do not know the previous day.

 

Creating a Virtual Environment in CMD

Creating a virtual environment can be a simple process in a Window environment.

First open Explorer and create a folder named autoTradeLesson in the appropriate folder.  

Open cmd and type the following command: 

cd C:\{directiory}\autoTradeLesson
c:\Python36-32{Python installation path}\python -m venv atvenv

 

The command will create a virtual environment. If successful, it will create a folder called atvenv in the autoTradeLesson folder.  

 

Activating a virtual environment in CMD  

To activate a virtual environment in a Windows CMD environment, use the autoTradeLesson   Inside the folder, type the following command:

atvenv\Scripts\activate.bat

If successful, the following Prompt will change, and this will succeed in activating the virtual environment.  

The fact that the virtual environment is activated means that any Directory in this CMD environment can now access Python set in the virtual environment.

If you enter Python in the state below, you will see that you are successfully entering the Python version that created the virtual environment.  

Applying a virtual environment in Pycharm  

Now let’s see how to apply a virtual environment in pycharm.  

Before applying,   autoTradeLesson   I will create another folder called autoTrade inside. &Nbsp;

Then open Pycharm and select Open File Or Project to add   autoTrade   Open the folder. &Nbsp;

And if you look at the left Project Navigator, you’ll see something like this: This will automatically apply the virtual environment to the project.  

When the Library root is in a different location  

In this development environment, we will be developing by adding a Python file to autoTradeLesson and. You may also create one more administrative folder. &Nbsp;

If you are working in the autoTrade folder as shown below  

If you select AutoTrade folder when opening in Pycharm, the virtual environment is not specified automatically. Then you can do the following:

First of all, in Pycharm, the File > Click on Settings and the following screen will appear. SelectProject Interpreteron the left and then down arrow to  Show All …  and click Show All ..  

Project Intereterswindow pops up. Press the ‘+’ button in the upper right corner.  

The following screen will appear. First we say New environment and we selectExisting environmentbecause we created the virtual environment on CMD. &Nbsp;

Next to the, you’ll see the Select Python interpreter popup, where the virtual environment is created,   Click atvenv /Scripts /python.exe, click OK, and when the popup disappears, press OK on the Add Python Interpreter OK button to exit the popup.  

After completing the above steps, the Project Intereters window will appear as shown below. There will be a phrase.

Now you can write code in Pycharm and run it to run in a virtual environment.

Let’s finish the development environment setting with this, and then we will try coding using the library.

You may also like...