Установка venv python pycharm

Configure a virtual environment

PyCharm makes it possible to use the virtualenv tool to create a project-specific isolated virtual environment . The main purpose of virtual environments is to manage settings and dependencies of a particular project regardless of other Python projects. virtualenv tool comes bundled with PyCharm, so the user doesn’t need to install it.

For Python 3.3+ the built-in venv module is used, instead of the third-party virtualenv utility.

Create a virtualenv environment

  1. Do one of the following:
    • Click the Python Interpreter selector and choose Add New Interpreter .
    • Press Control+Alt+S to open Settings and go to Project: | Python Interpreter . Click the Add Interpreter link next to the list of the available interpreters.
    • Click the Python Interpreter selector and choose Interpreter Settings . Click the Add Interpreter link next to the list of the available interpreters.
  2. Select Add Local Interpreter .
  3. In the left-hand pane of the Add Python Interpreter dialog, select Virtualenv Environment . Creating a virtual environment
  4. The following actions depend on whether you want to create a new virtual environment or to use an existing one. New virtual environment
    • Specify the location of the new virtual environment in the Location field, or click and browse for the desired location in your file system. The directory for the new virtual environment should be empty.
    • Choose the base interpreter from the list, or click Choose the base interpreterand find the desired Python executable in your file system.
    • Select the Inherit global site-packages checkbox if you want all packages installed in the global Python on your machine to be added to the virtual environment you’re going to create. This checkbox corresponds to the —system-site-packages option of the virtualenv tool.

    Existing virtual environment

    • Choose the desired interpreter from the list.
    • If the desired interpreter is not on the list, click , and then browse for the desired Python executable (for example, venv/bin/python on macOS or venv\Scripts\python.exe on Windows).
Читайте также:  Добавление записи в таблицу

The selected virtual environment will be reused for the current project.

If PyCharm displays the Invalid environment warning, it means that the specified Python binary cannot be found in the file system, or the Python version is not supported. Check the Python path and install a new version, if needed.

You can create as many virtual environments as required. To easily tell them from each other, use different names.

Use an existing interpreter

  1. Press Control+Alt+S to open the IDE settings and select Project | Python Interpreter .
  2. Expand the list of the available interpreters and click the Show All link. Alternatively, click the icon and select Show All . Show all available interpretersVirtual environments are marked with .
  3. Select the target environment from the list and click OK to confirm your choice. Select a virtual environment

PyCharm can create a virtual environment for your project based on the project requirements.

Create a virtual environment using the project requirements

Create a virtual environment using the requirements.txt file

  1. Open any directory with your source files that contains the requirements.txt or setup.py file: select File | Open from the main menu and choose the directory.
  2. If no virtual environment has been created for this project, PyCharm suggests creating it:
  3. Keep the suggested options, or specify the environment location or base Python interpreter. Click OK to complete the task.

The environment is created

Once you click OK , PyCharm creates an environment and installs all the required packages. On the completion, see the notification popup:

Warning with options for configuring a project interpreter

Note that if you ignore a suggestion to create a virtual environment, PyCharm won’t create a Python interperter for your project. So, any time when you open a .py file, you’ll see the warning with the options for configuring a project interpreter:

This approach is particularly helpful when you want to upgrade a version of Python your environment is based on, for example, from 3.5 to 3.9. You can specify a new base interpreter and use requirements.txt to ensure all the needed packages are installed.

For any of the configured Python interpreters (but Docker-based), you can:

Once you have create a new virtual environment, you can reuse it for your other projects. Learn more how to setup an existing environment as a Python interpreter.

Источник

How To Configure PyCharm venv

PyCharm is the go-to IDE for Pythonists, no matter what the Python project is. From Django to Flask, PyCharm’s extensive support makes it an attractive out-of-the-box solution.

venv – or “virtual environments” – is a Python module that’s used to create a lightweight and isolated environment for running Python projects. It’s used to keep the dependencies required by different projects isolated from one another.

To create your own venv in Python, the general flow of command you would need to enter looks something like this:

This will give you a popup screen that looks something like this:

In the search bar, look up virtualenv. The search panel will give you the virtualenv package for you to download and install. Click on the install button in the top right-hand corner.

Part 2: Setting up your project’s virtual environment in PyCharm

Head over to your top navigation bar, click on File, and then Settings. In the left-hand panel, open up the drop-down that says Project: . There will be an option called Python Interpreter.

Click on the little settings cog and select the Add option.

You will get a screen that looks like this:

If you are running lower than Python 3.3, you will need to select the dropdown for Base interpreter and find your downloaded venv package. If you are running Python 3.3+, then the option of your base interpreter is automatically chosen for you.

If this is an existing project and you want to import a pre-existing interpreter, you can do so by selecting Existing environment and then changing the Interpreter field to your settings.

Once you’ve done that, click on OK to create a new environment. You will get a progress bar in PyCharm’s IDE that updates you on the creation of the virtual environment.

Once completed, you will get a blank virtual environment with only pip and setuptools .

Part 3: Installing dependencies and packages

Every project requires some sort of dependency and packages to work. You have two options in PyCharm when it comes to installing dependencies and packages for your Python project’s virtual environment.

To do this, you can do so via the + sign in the Python Interpreter section, or via the Python Packages option at the bottom bar of your main IDE’s view.

Option 1 – Installing dependencies and packages for venv via PyCharm Python Interpreter

Clicking on the + sign inside the Python Interpreter will give you a screen of Available Packages. You can use the search bar to find the package and dependency you need for your particular project. For example, if you’re building a Django project, you can search up Django and it will give you the latest option available.

Click on Install Package to install the package as part of your venv requirements. When you click on Install Package, you will get an update on the screen to tell you that it is installing.

Once it is completed, you will get a green notification bar at the bottom of your Available Packages view.

To uninstall any packages you do not need or want, you can do so via the minus – sign on the main Python Interpreter screen.

Once it has successfully uninstalled the package from your venv, you will get a green success message at the bottom of the view.

Option 2 – Installing dependencies and packages for venv via Python Packages

Look at the bottom of your PyCharm IDE and select the option Python Packages. This will give you a popup of the currently installed packages.

In the search bar, look up the package you want to install. Here’s an example of how to install Django.

Click on the install button to add it to your packages and dependencies list. The install button will change to a loading bar. This will disappear once you’ve successfully installed the package to your virtual environment as a dependency.

Once completed, if you click on the x in the search bar, you will see all the packages that are in your Python project’s venv.

To uninstall the package from your Python’s virtual environment and remove it as a dependency, select the package you want and select on the triple dots option to your right. Click on delete.

Once it has been deleted from your project’s dependencies, the package will be removed from the list.

Wrap up

Setting up and managing your virtual environment in Python with venv is pretty simple in PyCharm. There are no terminal commands needed. It is great for both beginners and seasoned developers looking to supercharge their workflow, especially with PyCharm.

The following article is brought to you by Tabnine — an AI-powered tool that uses generative models to improve software development

The 6 best CodeWhisperer alternatives to know in 2022 (free and paid).

When to use and when not to use Confluence.

Revisiting end-to-end testing to improve reliability, speed, and development.

8 best podcasts for managing dev teams.

The 8 biggest software engineering challenges your team faces.

More from Python Tutorials

How to Configure Sublime Text 4 for Python Development

How to Create Django Projects in PyCharm Community Edition

How To Code In Python On Sublime Text

Top 10 Python Plugins for VS Code

Modern software is almost always dependent on external libraries. A software project rarely starts from…

We’ve searched high and low, scouring the Internet, hunting for the top 10 time-saving Python…

PyCharm is the IDE that many Pythonists choose over other alternatives like Visual Studio and…

Источник

Оцените статью