Invalid python sdk pycharm что делать
Перейти к содержимому

Invalid python sdk pycharm что делать

  • автор:

Configure a Python SDK

The following is only valid when the Python plugin is installed and enabled.

To develop Python scripts in IntelliJ IDEA, download and install Python and configure at least one Python SDK. A Python SDK can be specified as a Python interpreter for Python project.

IntelliJ IDEA supports:

To view the list of available SDKs, choose File | Project Structure from the main menu Control+Alt+Shift+S . Python SDKs can be configured on the following levels:

Show available Python SDKs

Change a Python SDK

Manage Python SDK modules

To easily tell them from each other, enter different names in the Name field. For more information about SDK configuration, refer to SDKs.

To add a Python SDK, you must configure a Python interpreter. Regardless of the level, you can configure a local or a remote Python interpreter.

Configuring local Python interpreters

To configure a local Python interpreter, adhere to one of the following procedures:

Configure a system interpreter

  1. Ensure that you have downloaded and installed Python on your computer. Installing Python on Windows from Microsoft Store If you are on Windows, you can download Python from the Microsoft Store and install it as a Python interpreter. Once the Python application is downloaded from the Microsoft Store, it becomes available in the list of the Python executables. Note that interpreters added from the Microsoft Store installations come with some limitations. Because of restrictions on Microsoft Store apps, Python scripts may not have full write access to shared locations such as TEMP and the registry.
  2. Navigate to File | Project Structure or press Control+Alt+Shift+S .
  3. In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  4. In the left-hand pane of the Add Python Interpreter dialog, select System Interpreter . Adding a system interpreter
  5. In the Interpreter drop-down, select one of the Python interpreters that have been installed in your system, or click and in the Select Python Interpreter dialog that opens, choose the desired Python executable. Selecting the Python executableYou will need admin privileges to install, remove, and upgrade packages for the system interpreter. When attempting to install an interpreter package through an intention action, you might receive the following error message: System Interpreter warning messageAs prompted, consider using a virtual environment for your project.
  6. Click OK to complete the task.

Create a virtualenv environment

  1. Navigate to File | Project Structure or press Control+Alt+Shift+S .
  2. In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  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 and 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.
    • Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in IntelliJ IDEA.

    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 IntelliJ IDEA 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.

Create a conda environment

  1. Ensure that Anaconda or Miniconda is downloaded and installed on your computer, and you’re aware of a path to its executable file. For more information, refer to the installation instructions.
  2. Navigate to File | Project Structure or press Control+Alt+Shift+S .
  3. In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  4. In the left-hand pane of the Add Python Interpreter dialog, select Conda Environment . New Conda environment
  5. The following actions depend on whether you want to create a new conda environment or to use an existing one. New conda environment
    • Specify the location of the new conda environment in the Location field, or click and browse for the desired location in your file system. The directory for the new conda environment should be empty.
    • Select the Python version from the list.
    • Normally, IntelliJ IDEA will detect conda installation. Otherwise, specify the location of the conda executable, or click to browse for it.
    • Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in IntelliJ IDEA.

    Existing conda environment

    • Select the environment from the list.
    • If the desired interpreter is not on the list, click, and then browse for the Python executable within the previously configured conda environment.
    • If necessary, specify the location of the conda executable, or click to browse for it.
    • Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in IntelliJ IDEA.

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

Create a pipenv environment

  1. Navigate to File | Project Structure or press Control+Alt+Shift+S .
  2. In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  3. In the left-hand pane of the Add Python Interpreter dialog, select Pipenv Environment . Adding a Pipenv environment
  4. Choose the base interpreter from the list, or click and find the desired Python executable in your file system.
  5. If your project contains Pipfile , you can choose whether you want to install the packages listed in it by enabling or disabling the Install packages from Pipfile checkbox. By default, the checkbox is enabled.
  6. If you have added the base binary directory to your PATH environmental variable, you don’t need to set any additional options: the path to the pipenv executable will be autodetected. If the pipenv executable is not found, follow the pipenv installation procedure to discover the executable path, and then specify it in the dialog.
  7. Click OK to complete the task.

When you have set the pipenv virtual environment as a Python interpreter, all available packages are added from the source defined in Pipfile . The packages are installed, removed, and updated in the list of the packages through pipenv rather than through pip.

Create a Poetry environment

  1. Navigate to File | Project Structure or press Control+Alt+Shift+S .
  2. In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  3. In the left-hand pane of the Add Python Interpreter dialog, select Poetry Environment . creating a poetry environment
  4. The following actions depend on whether you want to create a new Poetry environment or to use an existing one. New Poetry environment
  5. Select Poetry Environment .
  6. Choose the base interpreter from the list, or click and find the desired Python executable in your file system.
  7. If your project contains pyproject.toml , you can choose whether you want to install the packages listed in it by enabling or disabling the Install packages from pyproject.toml checkbox. By default, the checkbox is enabled.
  8. If IntelliJ IDEA doesn’t detect the poetry executable, specify the following path in the dialog, replacing jetbrains with your username:

/Users/jetbrains/Library/Application Support/pypoetry/venv/bin/poetry
C:\Users\jetbrains\AppData\Roaming\pypoetry\venv\Scripts\poetry.exe
/home/jetbrains/.local/bin/poetry

  • Make sure that the project directory contains a pyproject.toml file.
  • Select Existing environment . Then expand the Interpreter list and choose the desired interpreter.
  • If the desired interpreter is not on the list, click, and then browse for the Python executable within the previously configured Poetry environment.

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

Configuring remote Python interpreters

To configure a remote Python interpreter:

Configure a WSL interpreter

  1. Click the Windows button in the lower-left corner of the screen and start typing System Information . To ensure that your system works well with WSL, upgrade your Windows to the latest available version.
  2. Install the Windows Subsystem for Linux and initialize your Linux distribution as described in the WSL Installation Guide.
  3. If your Linux distribution doesn’t come with rsync, you need to install it:

sudo apt install rsync
sudo pacman -S rsync

When working with WSL 2, mind the following known WSL issues:

  • Add «allow» rule to Windows firewall for WSL2 network
  • stdin is explicitly closed for Python process
  • Debugger doesn’t work properly if firewall is not opened for WSL IP.
  • Navigate to File | Project Structure or press Control+Alt+Shift+S .
  • In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  • In the left-hand pane of the Add Python Interpreter dialog, select WSL .
  • Select the Linux distribution with the required Python interpreter.
  • In Python interpreter path field, specify the path to the Python executable. You can accept the default, type in a different path, or click to browse. Add a remote interpreter using WSL
  • Click OK . The configured remote interpreter is added to the list.
  • Configure an interpreter using Vagrant

    1. Ensure that the following prerequisites are met (outside of IntelliJ IDEA):
      • One of supported Vagrant providers is installed on your computer.
      • Vagrant is installed on your computer, and all the necessary infrastructure is created.
      • The parent folders of the following executable files have been added to the system PATH variable:
        • vagrant.bat or vagrant from your Vagrant installation. This should be done automatically by the installer.
        • VBoxManage.exe or VBoxManage from your Oracle’s VirtualBox installation.
      • The required virtual boxes are created.
    2. Make sure that the Vagrant plugin is installed and enabled.
    3. Ensure that you have properly initiated and started Vagrant. Basically, you need to open the Terminal window and execute the following commands:

    $ vagrant init ubuntu/trusty64
    $ vagrant up

    For more information, refer to Vagrant documentation.

  • Navigate to File | Project Structure or press Control+Alt+Shift+S .
  • In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  • In the left-hand pane of the Add Python Interpreter dialog, select Vagrant .
  • In Python interpreter path field, specify the path to the Python executable. You can accept the default, type in a different path, or click to browse. Add a remote interpreter using Vagrant
  • Click OK . The configured remote interpreter is added to the list.
  • Configure an interpreter using SSH

    You cannot use a Windows machine as a remote host when configuring SSH interpreters.

    1. Ensure that there is an SSH server running on a remote host, since IntelliJ IDEA runs remote interpreters via ssh-sessions.
    2. Navigate to File | Project Structure or press Control+Alt+Shift+S .
    3. In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
    4. Select New server configuration , then specify server information (host, port, and username). adding an interpreter via SSHAlternatively, you can select Existing server configuration and choose any available deployment configuration from the list. existing configurationsIf needed, click to review the Connection settings, Mappings, and Excluded paths for the selected deployment configuration. Click Next to continue configuring an interpreter.
    5. In the next dialog window, provide the authentication details to connect to the target server. specifying authentication detailsSelect Password or Key pair (OpenSSH or PuTTY) and enter your password or passphrase. If Key pair (OpenSSH or PuTTY) is selected, specify:
      • Private key : location of the file with a private key
      • Passphrase : similar to a password, it serves to encrypt the private key.

    Click Next to proceed.

    Setting SSH mappings

  • In the next dialog window, verify the path to the desired Python interpreter. You can accept the default, or specify a different one. You have to configure the path mappings between your local project and the server. To do that, click next to the Sync folders field and enter the path to the local project folder and the path to the folder on the remote server. You can also select the checkbox to enable automatic upload of the local changes to the remote server.
  • Click OK . The configured remote interpreter is added to the list.
  • Configure an interpreter using Docker

    1. Make sure that the following prerequisites are met:
      • Docker is installed, as described in the Docker documentation.
      • You have stable Internet connection, so that IntelliJ IDEA can download and run busybox:latest (the latest version of the BusyBox Docker Official Image). Once you have successfully configured an interpreter using Docker, you can go offline.

    Note that you cannot install any Python packages into Docker-based project interpreters.

  • Navigate to File | Project Structure or press Control+Alt+Shift+S .
  • In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  • In the left-hand pane of the Add Python Interpreter dialog, select Docker . Adding a Python interpreter using Docker
  • Select an existing Docker configuration in the Docker server dropdown. Alternatively, click New and perform the following steps to create a new Docker configuration: Create a Docker configuration Click to add a Docker configuration and specify how to connect to the Docker daemon. The connection settings depend on your Docker version and operating system. For more information, refer to Docker connection settings. The Connection successful message should appear at the bottom of the dialog. The Docker connection settings for interpreterFor more information about mapping local paths to the virtual machine running the Docker daemon when using Docker on Windows or macOS, refer to Virtual machine path mappings for Windows and macOS hosts. You will not be able to use volumes and bind mounts for directories outside of the mapped local path. This table is not available on a Linux host, where Docker runs natively and you can mount any directory to the container.
  • Specify the image and the path to the Python executable: Configuring a Python interpreter using Docker
  • Click OK . The configured remote interpreter is added to the list.
  • Configure an interpreter using Docker Compose

    1. Make sure that the following prerequisites are met:
      • Docker is installed, as described in the Docker documentation.
      • You have stable Internet connection, so that IntelliJ IDEA can download and run busybox:latest (the latest version of the BusyBox Docker Official Image). Once you have successfully configured an interpreter using Docker, you can go offline.

    Note that you cannot install any Python packages into Docker-based project interpreters.

  • Navigate to File | Project Structure or press Control+Alt+Shift+S .
  • In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
  • In the left-hand pane of the Add Python Interpreter dialog, select Docker Compose . Adding a Python interpreter using Docker Compose
  • Select an existing Docker configuration in the Server dropdown. Alternatively, click New and perform the following steps to create a new Docker configuration: Create a Docker configuration Click to add a Docker configuration and specify how to connect to the Docker daemon. The connection settings depend on your Docker version and operating system. For more information, refer to Docker connection settings. The Connection successful message should appear at the bottom of the dialog. The Docker connection settings for interpreterFor more information about mapping local paths to the virtual machine running the Docker daemon when using Docker on Windows or macOS, refer to Virtual machine path mappings for Windows and macOS hosts. You will not be able to use volumes and bind mounts for directories outside of the mapped local path. This table is not available on a Linux host, where Docker runs natively and you can mount any directory to the container.
  • In Configuration files , specify the docker-compose.yml file. Also select the service. Configuring a Python interpreter using Docker ComposeOptionally, specify environment variables and edit the Compose project name in Project name .
  • Finally, specify the path to the Python executable: Configuring a Python interpreter using Docker Compose
  • Click OK . The configured remote interpreter is added to the list.
  • For more information about remote Python interpreters, refer to Configure remote Python interpreters.

    Removing Python interpreters

    If you no longer need a Python interpreter for a project, you can remove it from the project settings.

    Remove a Python interpreter

    1. Navigate to File | Project Structure or press Control+Alt+Shift+S .
    2. In the Project Structure dialog, click SDKs node under Platform Settings .
    3. Choose the interpreter that you want to remove and click .

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

    • Manage interpreter paths
    • Install, uninstall, and upgrade Python packages

    Invalid Python SDK in PyCharm

    Since this morning, I’m no longer able to run projects in PyCharm. When generating a new virtual environment, I get an «Invalid Python SDK» error. Cannot set up a python SDK at Python 3.11. The SDK seems invalid. What I noticed: No matter what base interpreter I select (3.8, 3.9, 3.10) Pycharm always generates a Python 3.11 interpreter. I did completely uninstall PyCharm, as well as all my python installations and reinstalled everything. I also went through the «Repair IDE» option in PyCharm. I also removed and recreated all virtual environments. When I run «cmd» and type ‘python’ then python 3.10.1 opens without a problem. This morning, I installed a new antivirus software that did some checks and deleted some «unnecessary files» — maybe it is related (antivirus software is uninstalled again).

    asked Jan 11, 2022 at 9:36
    user7431005 user7431005
    4,149 4 4 gold badges 26 26 silver badges 55 55 bronze badges
    Seems to be creeping up with the 2021.3.2. I wish there was an issue to track this
    Mar 5, 2022 at 8:13

    12 Answers 12

    I had the same problem on Linux. Solved it by invalidating caches as suggested here: https://stackoverflow.com/a/45099651/3990607

    In pycharm click on File menu, then choose Invalidate caches. , tick all 4 boxes and then restart PyCharm. Solved the problem for me.

    Configure a Python interpreter

    To work with your Python code in PyCharm, you need to configure at least one Python interpreter. You can use a system interpreter that is available with your Python installation. You can also create a Virtualenv, pipenv, Poetry, or conda virtual environment . A virtual environment consists of a base interpreter and the installed packages.

    With PyCharm Professional , you can also configure interpreters to execute your Python code on remote environments by using SSH, Vagrant, Docker, Docker Compose, or WSL (only for Windows).

    When you configure a Python interpreter , you need to specify the path to the Python executable in your system. So, before configuring a Python interpreter, you need to ensure that you’ve downloaded Python and installed it in your system and you’re aware of a path to it.

    You can create several Python interpreters based on the same Python executable. This is helpful when you need to create different virtual environments for developing different types of applications. For example, you can create one virtual environment based on Python 3.6 to develop Django applications and another virtual environment based on the same Python 3.6 to work with scientific libraries.

    Python interpreters can be configured for a new project or for the current project (you can create a new interpreter or use one of the existing interpreters).

    Configuring an existing Python interpreter

    At any time, you can switch the Python interpreter either by using the Python Interpreter selector or in Settings .

    Switch the Python interpreter using the Python Interpreter selector

    Project interpreter selector

    • The Python Interpreter selector is located on the status bar. It is the most convenient and quickest way to switch the Python interpreter. Just click it and select the target interpreter:

    Switch the Python interpreter in the IDE settings

    View interpreters

    1. Press Control+Alt+S to open the IDE settings and then select Project | Python Interpreter .
    2. Click the drop-down and select the desired Python interpreter:
    3. If it’s not on the list, click Show All . Then select the desired interpreter in the left pane and click OK . When PyCharm stops supporting any of the outdated Python versions, the corresponding Python interpreter is marked as unsupported.

    When you change the project interpreter and select an SSH interpreter, you might need to synchronize the local content with the target server. Mind a notification balloon in the lower-right corner:

    Sync local files with the deployment server

    You can choose to enable the automatic uploading of files to the server:

    • Click Auto-upload files to start uploading on the next save.
    • Click Sync and auto-upload files to immediately sync the files and upload them on every save in future.

    Modify a Python interpreter

    1. Press Control+Alt+S to open the IDE settings and then select Project | Python Interpreter .
    2. Expand the list of the available interpreters and click Show All . Show all available interpreters
    3. You can modify the path to the Python executable in the Interpreter path field. When the Associate this virtual environment with the current project checkbox is enabled, the interpeter is available only in the current PyCharm project. To change the interpreter name, select the target interpreter and click . Edit iconThe Python interpreter name specified in the Name field, becomes visible in the list of available interpreters. Click OK to apply the changes. Edit interpreter settings

    Remove a Python interpreter

    If you no longer need a Python interpreter for a project, you can remove it from the project settings.

    1. Do one of the following:
      • Press Control+Alt+S to open the IDE settings and then select Project | Python Interpreter .
      • Click the Python Interpreter selector and choose Interpreter Settings .
    2. Expand the list of the available interpreters and click Show All . Show all available interpreters
    3. Choose the interpreter that you want to remove and click . Remove a Python interpreter

    Creating a new Python interpreter

    Configuring local Python interpreters

    To configure a local Python interpreter for the current project, follow one of the procedures below:

    Configure a system interpreter

    1. Ensure that you have downloaded and installed Python on your computer. Installing Python on Windows from Microsoft Store If you are on Windows, you can download Python from the Microsoft Store and install it as a Python interpreter. Once the Python application is downloaded from the Microsoft Store, it becomes available in the list of the Python executables. Note that interpreters added from the Microsoft Store installations come with some limitations. Because of restrictions on Microsoft Store apps, Python scripts may not have full write access to shared locations such as TEMP and the registry.
    2. 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.
    3. Select Add Local Interpreter .
    4. In the left-hand pane of the Add Python Interpreter dialog, select System Interpreter . Adding a system interpreter
    5. In the Interpreter drop-down, select one of the Python interpreters that have been installed in your system, or click and in the Select Python Interpreter dialog that opens, choose the desired Python executable. Selecting the Python executableYou will need admin privileges to install, remove, and upgrade packages for the system interpreter. When attempting to install an interpreter package through an intention action, you might receive the following error message: System Interpreter warning messageAs prompted, consider using a virtual environment for your project.
    6. Click OK to complete the task.

    Create a virtualenv environment

    Creating a virtual 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 .
    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 and 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.

    Create a conda environment

    New Conda environment

    1. Ensure that Anaconda or Miniconda is downloaded and installed on your computer, and you’re aware of a path to its executable file. For more information, refer to the installation instructions.
    2. 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.
    3. Select Add Local Interpreter .
    4. In the left-hand pane of the Add Python Interpreter dialog, select Conda Environment .
    5. The following actions depend on whether you want to create a new conda environment or to use an existing one. New conda environment
      • Select the Python version from the list.
      • Normally, PyCharm will detect conda installation. Otherwise, specify the location of the conda executable, or click to browse for it.
      • Specify the environment name.

      Existing conda environment

      • Select the environment from the list.

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

    Create a pipenv environment

    Adding a Pipenv 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 Pipenv Environment .
    4. Choose the base interpreter from the list, or click and find the desired Python executable in your file system.
    5. If your project contains Pipfile , you can choose whether you want to install the packages listed in it by enabling or disabling the Install packages from Pipfile checkbox. By default, the checkbox is enabled.
    6. If you have added the base binary directory to your PATH environmental variable, you don’t need to set any additional options: the path to the pipenv executable will be autodetected. If the pipenv executable is not found, follow the pipenv installation procedure to discover the executable path, and then specify it in the dialog.
    7. Click OK to complete the task.

    When you have set the pipenv virtual environment as a Python interpreter, all available packages are added from the source defined in Pipfile . The packages are installed, removed, and updated in the list of the packages through pipenv rather than through pip.

    Create a Poetry environment

    creating a poetry 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 Poetry Environment .
    4. The following actions depend on whether you want to create a new Poetry environment or to use an existing one. New Poetry environment
      • Select Poetry Environment .
      • Choose the base interpreter from the list, or click and find the desired Python executable in your file system.
      • If your project contains pyproject.toml , you can choose whether you want to install the packages listed in it by enabling or disabling the Install packages from pyproject.toml checkbox. By default, the checkbox is enabled.
      • If PyCharm doesn’t detect the poetry executable, specify the following path in the dialog, replacing jetbrains with your username:

    /Users/jetbrains/Library/Application Support/pypoetry/venv/bin/poetry
    C:\Users\jetbrains\AppData\Roaming\pypoetry\venv\Scripts\poetry.exe
    /home/jetbrains/.local/bin/poetry

    • Make sure that the project directory contains a pyproject.toml file.
    • Select Existing environment . Then expand the Interpreter list and choose the desired interpreter.
    • If the desired interpreter is not on the list, click, and then browse for the Python executable within the previously configured Poetry environment.

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

    Configuring remote Python interpreters

    When a remote Python interpreter is added, at first the PyCharm helpers are copied to the remote host. PyCharm helpers are needed to run remotely the packaging tasks, debugger, tests and other PyCharm features.

    Next, the skeletons for binary libraries are generated and copied locally. Also, all the Python library sources are collected from the Python paths on a remote host and copied locally along with the generated skeletons. Storing skeletons and all Python library sources locally is required for resolve and completion to work correctly.

    PyCharm checks the remote helpers version on every remote run, so if you update your PyCharm version, the new helpers will be uploaded automatically, and you don’t need to recreate remote interpreter.

    You can only add a remote interpreter for your Project . A remote interpreter cannot be set as the default interpreter for a Workspace .

    Configure a WSL interpreter

    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 On WSL .
    3. Wait until PyCharm detects Linux on your machine and completes introspection. Click Next to proceed: Detecting Linux
    4. In the left-hand pane of the dialog, select the type of the WSL interpreter you want to create: Virtual Environment , Conda Environment , or System Interpreter . New WSL interpreterFor a system interpreter, just provide the path to the Python executable in the selected Linux distribution. For virtual and conda environments, you can provide a path to a Python executable of an existing environment in the selected Linux distribution or create a new environment based on the specified Python.

    Once done, the new interpreter will be added to your project, and the default mnt mappings will be set.

    Configure an interpreter using Vagrant

    1. Ensure that the following prerequisites are met (outside of PyCharm):
      • One of supported Vagrant providers is installed on your computer.
      • Vagrant is installed on your computer, and all the necessary infrastructure is created.
      • The parent folders of the following executable files have been added to the system PATH variable:
        • vagrant.bat or vagrant from your Vagrant installation. This should be done automatically by the installer.
        • VBoxManage.exe or VBoxManage from your Oracle’s VirtualBox installation.
      • The required virtual boxes are created.
    2. Make sure that the Vagrant plugin is enabled.
    3. Ensure that you have properly initiated and started Vagrant. Basically, you need to open the Terminal window and execute the following commands:

    $ vagrant init ubuntu/trusty64
    $ vagrant up

    For more information, refer to Vagrant documentation.

  • 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.
  • Select On Vagrant .
  • Specify the path to the Vagrant instance folder in Vagrant Instance Folder . Wait until you see a link in Vagrant Host URL .
  • In the New Target: Vagrant dialog, click the browse icon next to the Vagrant Instance Folder field, and specify the desired Vagrant instance folder. This results in showing the link to Vagrant Host URL . Specifying the Vagrant instance folder
  • In the next field, PyCharm will display the path to the Python executable. Press «Next» to proceed. Python executable is discovered
  • You can create a virtual environment (venv or conda) or use a system Python interpreter for the target Vagrant instance. Note that virtual environment must be configured and available in the specified Vagrant instance folder. Otherwise, the corresponding lists will be empty. Virtual environment on a target Vagrant instanceClik Create to complete the task.
  • Configure an interpreter using SSH

    1. Ensure that there is an SSH server running on a remote host, since PyCharm runs remote interpreters via ssh-sessions.
    2. 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.
    3. Select On SSH .
    4. Select an option to create a new SSH connection, then specify server information (host, port, and username). adding an interpreter via SSHAlternatively, you can select Existing and choose any available SSH configuration from the list. To create a new SSH configuration, follow the steps below: Creating an SSH configuration
      • Click next to the list of configurations: Add new SSH configuration
      • Click, disable the Visible only for this project checkbox, and fill in the required fields: Adding new SSH configuration
      • Once done, the newly created SSH configuration will appear in the list of available configurations. It will also become available in the SSH Deployment Configurations settings. Click Next to proceed: Connecting to SSH server
    5. In the next dialog window, provide the authentication details to connect to the target server. specifying authentication detailsSelect Password or Key pair (OpenSSH or PuTTY) and enter your password or passphrase. If Key pair (OpenSSH or PuTTY) is selected, specify:
      • Private key : location of the file with a private key
      • Passphrase : similar to a password, it serves to encrypt the private key.

    Click Next to proceed.

  • Wait until PyCharm completes the introspection of the SSH server. SSH server introspection
  • In the next dialog, select a type of Python environment to configure on the SSH server. Selecting a Python environmentYou can create a new virtual environment, select an existing one, or use a system interpreter.
    • 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.
    • If you need to execute your Python code on the SSH server as a sudo user, enable the Execute code with root privileges via sudo checkbox.
    • You can configure the path mappings between your local project and the server. To do that, click the Browse icon in the Sync folders field and enter the path to the local project folder and the path to the folder on the remote server.

    Click Create to complete adding the interpreter.

    Configure an interpreter using Docker

    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 On Docker .
    3. Select an existing Docker configuration in the Docker server dropdown. Alternatively, click and perform the following steps to create a new Docker configuration: Create a Docker configuration Click to add a Docker configuration and specify how to connect to the Docker daemon. The connection settings depend on your Docker version and operating system. For more information, refer to Docker connection settings. The Connection successful message should appear at the bottom of the dialog. Docker connection settingsFor more information about mapping local paths to the virtual machine running the Docker daemon when using Docker on Windows or macOS, refer to Virtual machine path mappings for Windows and macOS hosts. You will not be able to use volumes and bind mounts for directories outside of the mapped local path. This table is not available on a Linux host, where Docker runs natively and you can mount any directory to the container.
    4. The following actions depend on whether you want to pull a pre-built image from a Docker registry or to build an image locally from a Dockerfile. Pull a Docker image Select Pull or use existing and specify the tag of the desired image in the Image tag field. Creating a Docker interpreter by pulling an imageBuild a Docker image Select Build and change the default values in the Dockerfile and Context folder fields if necessary. Creating a Docker interpreter by building an imageIf required, expand the Optional section and specify the following:
    Image tag Specify an optional name and tag for the built image. This can be helpful for referring to the image in the future. If you leave the field blank, the image will have only a random unique identifier.
    Build options Set supported docker build options. For example, you can specify metadata for the built image with the —label option.
    Build args Specify the values for build-time variables that can be accessed like regular environment variables during the build process but do not persist in the intermediate or final images. This is similar to using the —build-args option with the docker build command. These variables must be defined in the Dockerfile with the ARG instruction. For example, you can define a variable for the version of the base image that you are going to use:

    ARG PY_VERSION=latest FROM python:$PY_VERSION

    The PY_VERSION variable in this case will default to latest and the Dockerfile will produce an image with the latest available version of Python, unless you redefine it as a build-time argument. If you set, PY_VERSION=3.10 , Docker will pull python:3.10 instead, which will run a container with Python version 3.10. Redefining the PY_VERSION argument is similar to setting the following command-line option:

    —build-arg PY_VERSION=3.10

  • Wait for PyCharm to connect to the Docker daemon and complete the container introspection. Docker container introspection is completed
  • Next, select an interpreter to use in the Docker container. You can choose any virtualenv or conda environment that is already configured in the container or select a system interpreter. Selecting a system interpreter for a Docker target
  • Click OK . The configured remote interpreter is added to the list.
  • Configure an interpreter using Docker Compose

    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 On Docker Compose .
    3. Select an existing Docker configuration in the Server dropdown. Alternatively, select Create new and perform the following steps to create a new Docker configuration: Create a Docker configuration Click to add a Docker configuration and specify how to connect to the Docker daemon. The connection settings depend on your Docker version and operating system. For more information, refer to Docker connection settings. The Connection successful message should appear at the bottom of the dialog. Docker connection settingsFor more information about mapping local paths to the virtual machine running the Docker daemon when using Docker on Windows or macOS, refer to Virtual machine path mappings for Windows and macOS hosts. You will not be able to use volumes and bind mounts for directories outside of the mapped local path. This table is not available on a Linux host, where Docker runs natively and you can mount any directory to the container.
    4. In Configuration files , specify the docker-compose.yml file. Also select the service. Creating a new Docker Compose targetOptionally, specify environment variables and edit the Compose project name in Project name .
    5. Wait until PyCharm creates and configures a new target: Configuring a Docker Compose target
    6. Next, select an interpreter to use in the container. You can choose any virtualenv or conda environment that is already configured in the container, or select a system interpreter. Selecting a system interpreter for a Docker target
    7. Click OK . The configured remote interpreter is added to the list.

    Setting the default interpreter

    In PyCharm, you can specify an interpreter that will be automatically set for all newly created projects.

    1. Go to File | New Projects Setup | Settings for New Projects (on Window and Linux) or File | New Projects Setup | Preferences for New Projects (on macOS).
    2. Select Python Interpreter settings. Then either choose an existing interpreter from the Python interpreter list of click to add a new interpreter. Click OK to save the changes. The change will become effective for all newly created projects in PyCharm.

    Managing interpreter packages

    For each interpreter, you can install, upgrade, and delete Python packages. By default, PyCharm uses pip to manage project packages. For conda environments you can use the conda package manager.

    Managing packages

    PyCharm smartly tracks the status of packages and recognizes outdated versions by showing the number of the currently installed package version (column Version ), and the latest available version (column Latest version ). When a newer version of a package is detected, PyCharm marks it with the arrow sign and suggests to upgrade it.

    By default, the Latest version column shows only stable versions of the packages. If you want to extend the scope of the latest available versions to any pre-release versions (such as beta or release candidate ), click Show early releases .

    You can upgrade several packages at once. Hold Cmd (macOS) or Ctrl on (Unix or Windows), left-click to select several items in the list of packages, and then click Upgrade .

    See the detailed instructions:

    • How to install a package
    • How to upgrade the package
    • How to uninstall the package

    If you are looking for a more convenient way to search for Python packages, preview the documentation, and manage Python package repositories, try the Python Packages tool window. For more information, refer to Manage packages in the Python Packages tool window.

    Configure a virtual environment

    The following is only valid when the Python plugin is installed and enabled.

    IntelliJ IDEA 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 IntelliJ IDEA, so the user doesn’t need to install it.

    Create a virtualenv environment

    1. Navigate to File | Project Structure or press Control+Alt+Shift+S .
    2. In the Project Structure dialog, select SDKs under the Platform Settings section, click , and choose Add Python SDK from the popup menu. Adding a new Python SDK
    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 and 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.
      • Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in IntelliJ IDEA.

      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 IntelliJ IDEA 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.

    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:

    • Manage interpreter paths
    • Install, uninstall, and upgrade Python packages

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *