- Mastering OpenCV 4 with Python
- Alberto Fernández Villán
- 813字
- 2025-04-04 14:41:17
Python IDEs to create virtual environments with virtualenv
In the next section, we are going to create virtual environments with PyCharm, which is a Python IDE. But before doing that, we are going to discuss IDEs. An IDE is a software application that facilitates computer programmers with software development. IDEs present a single program where all the development is done. In connection with Python IDEs, two approaches can be found:
- General editors and IDEs with Python support
- Python-specific editors and IDEs
In the first category (general IDEs), some examples should be highlighted:
- Eclipse + PyDev
- Visual Studio + Python Tools for Visual Studio
- Atom + Python extension
In the second category, here are some Python-specific IDEs:
- PyCharm: One of the best full-featured, dedicated IDEs for Python. PyCharm installs quickly and easily on Windows, macOS, and Linux platforms. It is the de facto Python IDE environment.
- Spyder: Spyder, which comes with the Anaconda package manager distribution, is an open source Python IDE that is highly suited for data science workflows.
- Thonny: Thonny is intended to be an IDE for beginners. It is available for all major platforms (Windows, macOS, Linux), with installation instructions on the site.
In this case, we are going to install PyCharm (the de facto Python IDE environment) Community Edition. Afterwards, we are going to see how to create virtual environments using this IDE. PyCharm can be downloaded from https://www.jetbrains.com/pycharm/. PyCharm can be installed on Windows, macOS, and Linux:

After the installation of PyCharm, we are ready to use it. Using PyCharm, we can create virtual environments in a very simple and intuitive way.
After opening Pycharm, you can click Create New Project. If you want to create a new environment, you should click on Project Interpreter: New Virtualenv environment. Then click on New environment using Virtualenv. This can be seen in the next screenshot:

You should note that the virtual environment is named (by default in PyCharm) venv and located under the project folder. In this case, the project is named test-env-pycharm and the virtual environment, venv, is located at test-env-pycharm/venv. Additionally, you can see that the venv name can be changed according to your preferences.
When you click on the Create button, PyCharm loads the project and creates the virtual environment. You should see something like this:

After the project is created, you are ready to install a package with just a few clicks. Click on File, then click on Settings... (Ctrl + Alt + S). A new window will appear, showing something like this:

Now, click on Project: and select Project Interpreter. On the right-hand side of this screen, the installed packages are shown in connection with the selected Project Interpreter. You can change it on top of this screen. After selecting the appropriate interpreter (and, hence, the environment for your project), you can install a new package. To do so, you can search in the upper-left input box. In the next screenshot, you can see an example of searching for the numpy package:

You can install the package (latest version by default) by clicking on Install Package. You can also specify a concrete version, as can be seen in the previous screenshot:

After the installation of this package, we can see that we now have three installed packages on our virtual environment. Additionally, it is very easy to change between environments. You should go to Run/Debug Configurations and click on Python interpreter to change between environments. This feature can be seen in the next screenshot:

Finally, you may have noticed that, in the first step, of creating a virtual environment with PyCharm, options other than virtualenv are possible. PyCharm gives you the ability to create virtual environments using Virtualenv, Pipenv, and Conda:

We previously introduced Virtualenv and how to work with this tool for creating isolated Python environments for Python libraries.
Pyenv (https://github.com/pyenv/pyenv) is used to isolate Python versions. For example, you may want to test your code against Python 2.6, 2.7, 3.3, 3.4, and 3.5, so you will need a way to switch between them.
Conda (https://conda.io/docs/) is an open source package management and environment management system (provides virtual environment capabilities) that runs on Windows, macOS, and Linux. Conda is included in all versions of Anaconda and Miniconda.