How to create virtual environment python
How to create virtual environment python
Создание виртуальных окружений и установка библиотек для Python 3 в IDE PyCharm
Язык программирования Python считается достаточно простым. На нем легче и быстрее пишутся программы, по сравнению с компилируемыми языками программирования. Для Python существует множество библиотек, позволяющих решать практически любые задачи. Есть, конечно, и минусы и другие нюансы, но это отдельная тема.
Статья начинается с базовых вещей: с установки Python 3, инструментов разработки Pip и Virtualenv и среды разработки PyCharm в Windows и в Ubuntu. Для многих это не представляет трудностей и возможно, что уже всё установлено.
После чего будет то, ради чего задумывалась статья, я покажу как в PyCharm создавать и использовать виртуальные окружения и устанавливать в них библиотеки с помощью Pip.
Установка Python и Pip
Pip является менеджером пакетов для Python. Именно с помощью него обычно устанавливаются модули/библиотеки для разработки в виде пакетов. В Windows Pip можно установить через стандартный установщик Python. В Ubuntu Pip ставится отдельно.
Установка Python и Pip в Windows
Для windows заходим на официальную страницу загрузки, где затем переходим на страницу загрузки определенной версии Python. У меня используется Python 3.6.8, из-за того, что LLVM 9 требует установленного Python 3.6.
Во время установки ставим галочку возле Add Python 3.x to PATH и нажимаем Install Now:
Установка Python и Pip в Ubuntu
В Ubuntu установить Python 3 можно через терминал. Запускаем его и вводим команду установки. Вторая команда выводит версию Python.
Далее устанавливаем Pip и обновляем его. После обновления необходимо перезапустить текущую сессию (или перезагрузить компьютер), иначе возникнет ошибка во время вызова Pip.
Основные команды Pip
Рассмотрим основные команды при работе с Pip в командой строке Windows и в терминале Ubuntu.
Установка VirtualEnv и VirtualEnvWrapper
VirtualEnv используется для создания виртуальных окружений для Python программ. Это необходимо для избежания конфликтов, позволяя установить одну версию библиотеки для одной программы, и другу для второй. Всё удобство использования VirtualEnv постигается на практике.
Установка VirtualEnv и VirtualEnvWrapper в Windows
В командной строке выполняем команды:
Установка VirtualEnv и VirtualEnvWrapper в Ubuntu
Для Ubuntu команда установки будет следующей:
После которой в конец
Работа с виртуальным окружением VirtualEnv
Рассмотрим основные команды при работе с VirtualEnv в командой строке Windows и в терминале Ubuntu.
Команда | Описание |
---|---|
mkvirtualenv env-name | Создаем новое окружение |
workon | Смотрим список окружений |
workon env-name | Меняем окружение |
deactivate | Выходим из окружения |
rmvirtualenv env-name | Удаляем окружение |
Установка PyCharm
PyCharm — интегрированная среда разработки для языка программирования Python. Обладает всеми базовыми вещами необходимых для разработки. В нашем случае огромное значение имеет хорошее взаимодействие PyCharm с VirtualEnv и Pip, чем мы и будем пользоваться.
Установка PyCharm в Windows
Скачиваем установщик PyCharm Community для Windows с официального сайта JetBrains. Если умеете проверять контрольные суммы у скаченных файлов, то не забываем это сделать.
В самой установке ничего особенного нету. По сути только нажимаем на кнопки next, и в завершение на кнопку Install. Единственно, можно убрать версию из имени папки установки, т.к. PyCharm постоянно обновляется и указанная версия в будущем станет не правильной.
Установка PyCharm в Ubuntu
Скачиваем установщик PyCharm Community для Linux с официального сайта JetBrains. Очень хорошей практикой является проверка контрольных сумм, так что если умеете, не ленитесь с проверкой.
Теперь в директории
Далее выполняем команды в терминале:
Производим установку. И очень важно в конце не забыть создать desktop файл для запуска PyCharm. Для этого в Окне приветствия в нижнем правом углу нажимаем на Configure → Create Desktop Entry.
Установка PyCharm в Ubuntu из snap-пакета
PyCharm теперь можно устанавливать из snap-пакета. Если вы используете Ubuntu 16.04 или более позднюю версию, можете установить PyCharm из командной строки.
Использование VirtualEnv и Pip в PyCharm
Поддержка Pip и Virtualenv в PyCharm появилась уже довольно давно. Иногда конечно возникают проблемы, но взаимодействие работает в основном стабильно.
Рассмотрим два варианта работы с виртуальными окружениями:
Первый пример: использование собственного виртуального окружения для проекта
Создадим программу, генерирующую изображение с тремя графиками нормального распределения Гаусса Для этого будут использоваться библиотеки matplotlib и numpy, которые будут установлены в специальное созданное виртуальное окружение для программы.
Запускаем PyCharm и окне приветствия выбираем Create New Project.
В мастере создания проекта, указываем в поле Location путь расположения создаваемого проекта. Имя конечной директории также является именем проекта. В примере директория называется ‘first_program’.
Теперь установим библиотеки, которые будем использовать в программе. С помощью главного меню переходим в настройки File → Settings. Где переходим в Project: project_name → Project Interpreter.
Здесь мы видим таблицу со списком установленных пакетов. В начале установлено только два пакета: pip и setuptools.
Справа от таблицы имеется панель управления с четырьмя кнопками:
Для добавления (установки) библиотеки в окружение нажимаем на плюс. В поле поиска вводим название библиотеки. В данном примере будем устанавливать matplotlib. Дополнительно, через Specify version можно указать версию устанавливаемого пакета и через Options указать параметры. Сейчас для matplotlib нет необходимости в дополнительных параметрах. Для установки нажимаем Install Package.
После установки закрываем окно добавления пакетов в проект и видим, что в окружение проекта добавился пакет matplotlib с его зависимостями. В том, числе был установлен пакет с библиотекой numpy. Выходим из настроек.
Теперь мы можем создать файл с кодом в проекте, например, first.py. Код программы имеет следующий вид:
Далее указываем в поле Name имя конфигурации и в поле Script path расположение Python файла с кодом программы. Остальные параметры не трогаем. В завершение нажимаем на Apply, затем на OK.
Теперь можно выполнить программу и в директории с программой появится файл gauss.png :
Второй пример: использование предварительно созданного виртуального окружения
Данный пример можно использовать во время изучения работы с библиотекой. Например, изучаем PySide2 и нам придется создать множество проектов. Создание для каждого проекта отдельного окружения довольно накладно. Это нужно каждый раз скачивать пакеты, также свободное место на локальных дисках ограничено.
Более практично заранее подготовить окружение с установленными нужными библиотеками. И во время создания проектов использовать это окружение.
В этом примере мы создадим виртуальное окружения PySide2, куда установим данную библиотеку. Затем создадим программу, использующую библиотеку PySide2 из предварительно созданного виртуального окружения. Программа будет показывать метку, отображающую версию установленной библиотеки PySide2.
Далее в созданном окружении устанавливаем пакет с библиотекой PySide2, также как мы устанавливали matplotlib. И выходим из настроек.
Теперь мы можем создавать новый проект использующий библиотеку PySide2. В окне приветствия выбираем Create New Project.
Для проверки работы библиотеки создаем файл second.py со следующий кодом:
Далее создаем конфигурацию запуска программы, также как создавали для первого примера. После чего можно выполнить программу.
Заключение
У меня нет богатого опыта программирования на Python. И я не знаком с другими IDE для Python. Поэтому, возможно, данные IDE также умеют работать с Pip и Virtualenv. Использовать Pip и Virtualenv можно в командой строке или в терминале. Установка библиотеки через Pip может завершиться ошибкой. Есть способы установки библиотек без Pip. Также создавать виртуальные окружения можно не только с помощью Virtualenv.
В общем, я лишь поделился небольшой частью опыта из данной области. Но, если не вдаваться в глубокие дебри, то этого вполне достаточно знать, чтобы писать простые программы на Python с использованием сторонних библиотек.
Python Virtual Environment | Introduction
A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.
Why do we need a virtual environment?
Imagine a scenario where you are working on two web based python projects and one of them uses a Django 1.9 and the other uses Django 1.10 and so on. In such situations virtual environment can be really useful to maintain dependencies of both the projects.
When and where to use a virtual environment?
By default, every project on your system will use these same directories to store and retrieve site packages (third party libraries). How does this matter? Now, in the above example of two projects, you have two versions of Django. This is a real problem for Python since it can’t differentiate between versions in the “site-packages” directory. So both v1.9 and v1.10 would reside in the same directory with the same name. This is where virtual environments come into play. To solve this problem, we just need to create two separate virtual environments for both the projects.The great thing about this is that there are no limits to the number of environments you can have since they’re just directories containing a few scripts.
Virtual Environment should be used whenever you work on any Python based project. It is generally good to have one new virtual environment for every Python based project you work on. So the dependencies of every project are isolated from the system and each other.
How does a virtual environment work?
We use a module named virtualenv which is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.
Installing virtualenv
Test your installation:
Using virtualenv
You can create a virtualenv using the following command:
After running this command, a directory named my_name will be created. This is the directory which contains all the necessary executables to use the packages that a Python project would need. This is where Python packages will be installed.
If you want to specify Python interpreter of your choice, for example Python 3, it can be done using the following command:
To create a Python 2.7 virtual environment, use the following command:
Now after creating virtual environment, you need to activate it. Remember to activate the relevant virtual environment every time you work on the project. This can be done using the following command:
Once the virtual environment is activated, the name of your virtual environment will appear on left side of terminal. This will let you know that the virtual environment is currently active. In the image below, venv named virtual environment is active.
Now you can install dependencies related to the project in this virtual environment. For example if you are using Django 1.9 for a project, you can install it like you install other packages.
The Django 1.9 package will be placed in virtualenv_name folder and will be isolated from the complete system.
Once you are done with the work, you can deactivate the virtual environment by the following command:
Now you will be back to system’s default Python installation.
This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Creating a Virtual Environment
This lesson covers how to create a virtual environment in a project folder. You learned that following the steps below will install a self contained Python environment in your project directory:
Andreas Schipplock on April 7, 2019
Hi, is this still considered a best-practice to create the venv inside the project? I somewhere read it’s advised to create all of your venvs inside
I’m just curious :).
Thanks in advance.
Dan Bader RP Team on April 8, 2019
@Andreas: I still create all of my venvs that way, I think it’s more of a “philosophical” point as to which practice is better (venv in project folder vs venv in global folder like
What I would avoid is having shared global venvs that are used by multiple projects. It might make sense under some circumstances, like when you have several scripts/apps that all use the same set of dependencies.
But over time and as these projects evolve it can become a hassle to keep the shared venv clean. I find per-project venvs much easier to manage over the long term.
Hope this helps you out!
paulakula11 on May 22, 2019
Dan Bader RP Team on May 23, 2019
@paulakula11: Thanks for pointing out that Ubuntu requires installing the python3-venv package 🙂
Ahmad Mayahi on May 23, 2019
Hi Dan, just a qucick question, I see many people are installing venv as follows:
So, they specify the version of the venv module, is that considered a best practice? or they just want to be more percise?
Dan Bader RP Team on May 23, 2019
It might make sense if you’re running or testing a project on multiple Python versions. I typically only use a single venv folder so the steps to activate the virtual environment are the same between all of my projects. When I’m testing with multiple versions of Python I’ll use something like tox to automate the process.
Jet on June 13, 2019
csharma19 on Aug. 3, 2019
Thanks for making this video…what additional steps would be needed if I’m using VS Code (via Anaconda)? I keep running into all sorts of issues (haven’t even gotten to testing the set up for a virtual environment yet. but am hesitant to try before I fix the existing issues)…
hung20736 on Dec. 9, 2019
When i do : apt-get install python3-venv. It again triggers
Im using Windows Subsystem for Linux.
bmorton on Dec. 28, 2019
Hi Dan, Can you or someone else on your team make a complete virtual environment tutorial for Windows? And maybe incorporating Anaconda/Jupyter Notebooks? It seems different enough to merit its own video. Thanks!
Dan Bader RP Team on Dec. 29, 2019
@bmorton: That’s a great idea—I’d love to go back and expand this course or do a more Windows-specific version in the future.
bmorton on Dec. 29, 2019
Great! Can you DM me on Slack when it is in the works? Been struggling with setting up a venv for a minute now, can’t find a good tutorial for Windows. Thanks!
DJ on Jan. 12, 2020
For windows systems (Python 3.8), the lessons learned are:
To activate, try
Thanks for the great series of video tutorials.
Dan Bader RP Team on Jan. 13, 2020
Thanks @DJ, I’ve updated your comments as requested. Really appreciate that you went back and took the time to share your findings with us. It really helps other course participants 🙂
andresgtn on April 11, 2020
Great tutorial. I’m struggling to find one that shows best practices for virtual environments and git repos. Do you create the venv inside the repo or the repo inside the venv? How do you keep track of all dependencies and push them to the repo? I’m fairly new to all this environment management and want a way to keep projects clean. Maybe theres a tutorial in this site already which you could point me to. Thx
Ricky White RP Team on April 12, 2020
Gregory Klassen on April 17, 2020
viraj7k on May 8, 2020
hey Dan, the command you use are same for windows? if different could you please mention it
tom77-ai on Feb. 14, 2021
Hi Dan, most of the commands are not working for the windows shell using python 3.9 Is there an overview what works here?
Ricky White RP Team on Feb. 15, 2021
Check the other comments above and you’ll see the Windows version of the commands you need.
John Paul CollabEdge on May 29, 2021
Bartosz Zaczyński RP Team on May 31, 2021
@John Paul CollabEdge virtualenv is a third-party library that used to be popular for managing virtual environments in Python. You can continue using it, but a subset of it has been integrated into the standard library under the venv module in Python 3.3, which is the recommended and portable way.
Charlie Grover on Sept. 11, 2021
I cannot activate using the responses up top of this post. I am running Python 3.9
In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:
I have tried every combination of the python line to no avail. please help.
Martin Breuss RP Team on Sept. 13, 2021
Hi @Charlie Grover, what operating system are you working on?
Check out the different commands for activating your venv described in the official Python documentation.
h602421365 on Dec. 11, 2021
I use the command prompt and it says that which is not recognized
Bartosz Zaczyński RP Team on Dec. 13, 2021
@h602421365 which is a shell command available on Unix-like operating systems, including macOS and Linux distributions like Ubuntu. If you’re on Windows, then you might try the equivalent where command.
Pipenv & Virtual Environments¶
This tutorial walks you through installing and using Python packages.
It will show you how to install and use the necessary tools and make strong recommendations on best practices. Keep in mind that Python is used for a great many different purposes, and precisely how you want to manage your dependencies may change based on how you decide to publish your software. The guidance presented here is most directly applicable to the development and deployment of network services (including web applications), but is also very well suited to managing development and testing environments for any kind of project.
This guide is written for Python 3, however, these instructions should work fine on Python 2.7—if you are still using it, for some reason.
Make sure you’ve got Python & pip¶
Before you go any further, make sure you have Python and that it’s available from your command line. You can check this by simply running:
If you’re newcomer and you get an error like this:
It’s because this command is intended to be run in a shell (also called a terminal or console). See the Python for Beginners getting started tutorial for an introduction to using your operating system’s shell and interacting with Python.
Additionally, you’ll need to make sure you have pip available. You can check this by running:
If you installed Python from source, with an installer from python.org, or via Homebrew you should already have pip. If you’re on Linux and installed using your OS package manager, you may have to install pip separately.
Installing Pipenv¶
Pipenv is a dependency manager for Python projects. If you’re familiar with Node.js’ npm or Ruby’s bundler, it is similar in spirit to those tools. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases.
Use pip to install Pipenv:
expanded to the absolute path to your home directory) so you’ll need to add
Installing packages for your project¶
Pipenv manages dependencies on a per-project basis. To install packages, change into your project’s directory (or just an empty directory for this tutorial) and run:
Pipenv will install the excellent Requests library and create a Pipfile for you in your project’s directory. The Pipfile is used to track which dependencies your project needs in case you need to re-install them, such as when you share your project with others. You should get output similar to this (although the exact paths shown will vary):
Using installed packages¶
Now that Requests is installed you can create a simple main.py file to use it:
Then you can run this script using pipenv run :
You should get output similar to this:
Next steps¶
Congratulations, you now know how to install and use Python packages! ✨ 🍰 ✨
Lower level: virtualenv¶
virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.
It can be used standalone, in place of Pipenv.
Install virtualenv via pip:
Test your installation:
Basic Usage¶
virtualenv venv will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was venv ) can be anything; omitting the name will place the files in the current directory instead.
You can also use the Python interpreter of your choice (like python2.7 ).
or change the interpreter globally with an env variable in
The name of the current virtual environment will now appear on the left of the prompt (e.g. (venv)Your-Computer:project_folder UserName$ ) to let you know that it’s active. From now on, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.
For Windows, the same command mentioned in step 1 can be used to create a virtual environment. However, activating the environment requires a slightly different command.
Assuming that you are in your project directory:
Install packages using the pip command:
This puts you back to the system’s default Python interpreter with all its installed libraries.
After a while, though, you might end up with a lot of virtual environments littered across your system, and it’s possible you’ll forget their names or where they were placed.
Python has included venv module from version 3.3. For more details: venv.
Other Notes¶
In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages. To do this, run:
This can help ensure consistency across installations, across deployments, and across developers.
Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list (see Version Control Ignores ).
virtualenvwrapper¶
virtualenvwrapper provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place.
To install (make sure virtualenv is already installed):
For Windows, you can use the virtualenvwrapper-win.
To install (make sure virtualenv is already installed):
In Windows, the default path for WORKON_HOME is %USERPROFILE%\Envs
Basic Usage¶
This creates the project_folder folder inside
virtualenvwrapper provides tab-completion on environment names. It really helps when you have a lot of environments and have trouble remembering their names.
workon also deactivates whatever environment you are currently in, so you can quickly switch between environments.
Other useful commands¶
virtualenv-burrito¶
With virtualenv-burrito, you can have a working virtualenv + virtualenvwrapper environment in a single command.
direnv¶
Install it on Mac OS X using brew :
On Linux follow the instructions at direnv.net
This opinionated guide exists to provide both novice and expert Python developers a best practice handbook to the installation, configuration, and usage of Python on a daily basis.
O’Reilly Book
This guide is now available in tangible book form!
All proceeds are being directly donated to the DjangoGirls organization.
Python Virtual Environment “venv” Cheat Sheet
Table of Contents
Virtual Environments in Python
How does the tool venv work?
The venv module is the new default way of creating basic virtual environments for new Python versions > 3.3. If you dive into virtual environments, you’ll quickly realize that there are a multitude of tools out there such as “ virtualenv “, “ pyenv “, and many more.
My recommendation for data scientists and beginners is the tool conda that comes with the Anaconda Python distribution. I’ve written an article about the concepts of virtual environments in Python, including a tutorial on how to use conda for your own projects:
The “ venv ” tool is the de-facto standard that is already preinstalled with your Python 3.3+ installation. You should learn this tool first—probably you can write Python code for many years before you are forced to touch another virtual environment tool.
Let’s start slowly: Python is a program such as everything else running on your computer. Programs are compiled into machine-readable binary code that is stored in a file. Hence, Python is nothing but a compiled binary file that you can execute on your computer just like Tetris or Minesweeper. If you run the command “ python ” in your shell, the binary is executed by your operating system.
Note: you may have to explicitly specify the location (path) of the Python binary file in your operating systems “environment variables” so that your computer can find the program “ python “.
Test whether your Python installation works correctly by opening a shell and typing “ python “.
The default way of working on your code project is as follows:
The problem is that all your projects share the same globally installed libraries. But some of them may require different versions or incompatible libraries. Also, you don’t want to clutter your Python installation with hundreds of external libraries.
This is where virtual environments come into play. A virtual environment serves as a “sandbox” for your Python program. You can install any external library or version there without having any global impact. The virtual environments are isolated, independent, and separate.
Crash Course venv
So, how to create a virtual environment using the venv tool?
How to Create Virtual Environments with Python “venv”?
The simple answer lies in the following code snippet:
The placeholder “ ve ” is simply the path to the virtual environment you want to create. In practice, it’ll be the path to the folder of your Python project that should be executed under the virtual environment.
The code snippet does multiple things: it creates a folder that contains a copy of the Python program itself. This means that any package you install within the virtual environment is not visible to your global Python installation.
Activate Your Virtual Environment
Now, the only thing left is to activate your virtual environment using the command (Bash):
Or the command (Win):
Now, you can simply execute “ python ” in your shell, and all programs you execute there will be executed within the Python virtual environment.
How to Install Libraries in Your Virtual Environment?
That’s easy, simply use the pip tool to install packages after you have activated the virtual environment.
It will automatically detect that you are currently in a virtual environment (as you have activated the environment).
How to Deactivate Your Virtual Environment
You can simply deactivate the virtual environment by typing the command:
Next, I’ll show you the best virtual environment cheat sheets on the web! 🙂
Most Comprehensive Virtualenv Cheat Sheet
The most simple and straightforward virtualenv cheat sheet was created by Aaron Lelevier. This is the screenshot from this site:
Virtualenv Cheat Sheet from Michael Noll
The following cheat sheet is pretty concise too—but doesn’t contain the same amount of information as the previous one.
Here’s a screenshot from this site:
Cheatography Virtual Environment Cheat Sheet
A nice cheat sheet is provided on the helpful cheatography website here. You can find a screenshot with the most relevant information next:
Quick and Easy Virtualenv Cheat Sheet
This cheat sheet almost doesn’t deserve the name–but it’s so concise that I just couldn’t resist including it here:
Dan’s Cheat Sheet
Finally, let’s end this cheat sheet collection with another useful one from Dan Poirier (source):
Summary
If you love cheat sheets, feel free to check out my 100% free Python email course with 11+ Python cheat sheets for learning and relearning the most important Python concepts:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.