- Configure an interpreter using command line
- Launch PyCharm with a pre-configured Python interpreter
- Configure a virtual environment
- Create a virtualenv environment
- Create a virtual environment using the project requirements
- Configuring a Local Python Interpreter
- New Interpreter
- Existing Interpreter
- Clone project
- Open project
- Conclusion
- Video
Configure an interpreter using command line
When launching PyCharm from the command line, you can specify which Python interpreter to use for new and existing projects.
Launch PyCharm with a pre-configured Python interpreter
You can find the executable for running PyCharm in the installation directory under bin . To use this executable as the command-line launcher, add it to your system PATH as described in Command-line interface.
- In the command line, execute the following command to set the PYCHARM_PYTHON_PATH variable:
You can open an existing project by providing the path to the project folder when you launch PyCharm. For more information, see Open files from the command line.
By default, PyCharm does not provide a command-line launcher. For information about creating a launcher script for PyCharm, see Command-line interface.
- In the command line, execute the following command to set the PYCHARM_PYTHON_PATH variable:
You can open an existing project by providing the path to the project folder when you launch PyCharm. For more information, see Open files from the command line.
You can find the script for running PyCharm in the installation directory under bin . To use this script as the command-line launcher, create a symbolic link in a directory from the PATH environment variable as described in Command-line interface.
- In the command line, execute the following command to set the PYCHARM_PYTHON_PATH variable:
You can open an existing project by providing the path to the project folder when you launch PyCharm. For more information, see Open files from the command line.
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
- 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 Add Local Interpreter .
- In the left-hand pane of the Add Python Interpreter dialog, select Virtualenv Environment .
- 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.
You can create as many virtual environments as required. To easily tell them from each other, use different names.
PyCharm can create a virtual environment for your project based on the project requirements.
Create a virtual environment using the project requirements
- 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.
- If no virtual environment has been created for this project, PyCharm suggests creating it:
- Keep the suggested options, or specify the environment location or base Python interpreter. Click OK to complete the task.
Once you click OK , PyCharm creates an environment and installs all the required packages. On the completion, see the notification popup:
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.
Configuring a Local Python Interpreter
How to set up a local Python interpreter for your project.
In the previous step, we customized the IDE. In this step we’re going to learn how to set up a Python interpreter using PyCharm.
New Interpreter
When we’re talking about local interpreters, PyCharm lets us configure them in two main ways:
- You can select and use your system interpreter
- Or you can create a virtual environment using virtualenv, Pipenv, Conda, or Poetry
You just need to have a Python installation already setup on your local machine. PyCharm lets you select your interpreter at three different points.
Firstly, you can select the interpreter when you create a new project. We already covered how to create a new project, but we skimmed the Python interpreter configuration.
When you create a new project, you need to choose between creating a new interpreter or selecting a previously configured interpreter. Let’s create a new one.
The first thing you will need to define is what kind of Python packaging manager you want to use. PyCharm comes with support for virtualenv, Pipenv, Conda and Poetry out of the box. Let’s use virtualenv. PyCharm will automatically identify the location where the venv folder will be created.
This new interpreter will be based on an existing one. In my case, this is Python 3.10. You can also choose another base interpreter from the list, or click the three dots to find another Python executable in your file system.
Secondly You can also create a virtualenv using Pipenv. Getting Pipenv installed on your system is an extra step. Once setup and visible to PyCharm, you can select it from the drop-down.
Finally, you can also create a virtual environment using Conda. Make sure that Anaconda or Miniconda is downloaded and installed on your computer then pick Conda from the dropdown, check if the location is correct. Select the Python version, then ensure that the Conda executable path is correct. The environment will be automatically created within your Anaconda installation.
We just saw making a new interpreter and the various Python standards for doing so.
Existing Interpreter
That’s how you create a new virtual environment in PyCharm when setting up a new project, but we can also select an existing Python project, that we haven’t yet opened as a PyCharm project.
From the New Project screen, click Previously configured interpreter. If you already have interpreters previously configured you can just pick the one you would like to use from this dropdown. If you see a no interpreter message here you can click the three dots.
Then, select it from the different options, including virtualenv environments, conda environments, pipenv interpreters, or even your system interpreter.
Clone project
The second place you can configure your interpreter is when you clone a project from VCS. In the PyCharm Getting Started screen, you can click Get from VCS button.
We can paste the repository URL in and accept the generated choice for the directory to clone into, and click Clone.
PyCharm opens a new project in that directory and starts cloning the repository. Once the clone has finished, PyCharm makes a project interpreter, based on your default Python, and starts installing the packages in your requirements file.
Open project
We have a cloned project and PyCharm has a “project interpreter” for it, but what if you want to change the interpreter used by a project? You can see which interpreter the project is using in the bottom-right of the screen. There are two ways to change this.
You can click the interpreter name in the Status Bar which opens a list of available interpreters. It will also give you the opportunity to either open interpreter settings or add a new interpreter.
Alternatively, you can find the same settings in your Preferences/Settings (⌘, | Ctrl+Alt+S) and then search for project interpreter. If you click the gear icon to the right of the drop-down, PyCharm will give you options for managing all the project interpreters that PyCharm knows about.
Conclusion
That’s it! We just went through configuring local Python interpreters in PyCharm.
In the next step, we’ll see how to configure remote interpreters in PyCharm.
Video
You can also check out the video for this step from our Getting Started series on YouTube: