JetBrains Fleet 1.42 Help

Running code using a virtual environment

This article demonstrates the use of Python's venv module to create and manage isolated virtual environments. It includes setting up a virtual environment, installing dependencies, freezing dependencies to a requirements.txt file, and running a sample Python script that uses an external library.

  • Isolation: ensures that the project's dependencies do not interfere with other projects.

  • Portability: easily share and recreate the environment on different machines.

  • Dependency management: use pip to install and manage project-specific dependencies.

  • Reproducibility: share the exact environment setup using requirements.txt.

Step 1: Set up a project

  1. Create a project directory and open the directory in JetBrains Fleet.

  2. Select View | Terminal from the main menu or press ⌃ ⇧ ` to open the Terminal view.

  3. In the Terminal tool, type the following command to create a virtual environment.

    python -m venv venv
  4. Press .

    The venv module creates a virtual environment named venv.

    python -m venv venv

Step 2: Activate the virtual environment

  • In the Terminal tool, type and run the following command to activate the virtual environment based on your operating system.

    • On Windows:

      venv\Scripts\activate
    • On macOS and Linux:

      source venv/bin/activate
    Activate the virtual environment

Step 3: Install dependencies

  1. In the Terminal tool, type and run the following command to install the requests package within the virtual environment.

    pip install requests
  2. To verify the installation, you can run the following command in the Terminal tool.

    pip list

    The Terminal tool should display requests in the list of installed packages.

    Install dependencies

Step 4: Capture installed dependencies

  • In the Terminal tool, type and run the following command to create a requirements.txt file to capture the installed packages.

    pip freeze > requirements.txt
    Capture installed dependencies

Step 5: Create a Sample Python Script

  1. Select View | Files from the main menu to open the Files view.

  2. Select File | New File from the main menu and name the file as main .py.

  3. Copy and paste the following code to main.py.

    import requests response = requests.get("https://api.github.com") print(response.json())
    Fleet Create A Sample Python Script

Step 6: Share your project

  • Share Python files from your project and requirements.txt. Consider the following example on github.com.

    The project has the following structure:

    The project has the following structure:

    pythonVenvDemo/ ├── README.md ├── main.py └── requirements.txt

Step 6: Recreate the Environment (on a different machine)

  1. Clone or copy the project directory.

    If you use the example from this tutorial, you can run the following command in the Terminal view:

    git clone https://github.com/apronichev/pythonVenvDemo.git
  2. In the directory pythonVenvDemo of the cloned project, run the following command in the Terminal view to create a new virtual environment:

    python -m venv venv
  3. In the Terminal tool, type and run the following command to activate the virtual environment:

    • On Windows:

      venv\Scripts\activate
    • On macOS and Linux:

      source venv/bin/activate
  4. In the Terminal tool, type and run the following command to install dependencies:

    pip install -r requirements.txt

Tips and tricks

Selecting the Python interpreter

  1. Press ⌘ , to open settings and select the tab with the name of your project (for example, pythonVenvDemo).

  2. Navigate to Toolchains | Python.

  3. From the Interpreter drop-down menu, select the Python interpreter that your need.

    Selecting the Python interpreter

Setting the custom path to the Conda executable

JetBrains Fleet can automatically detect all existing Conda environments. For this feature to work, Conda must be installed in a well-known directory or added to the PATH variable. If these conditions are not met, you can manually specify the Conda executable using the conda.executable settings key.

  1. Navigate to Goto | Action in the main menu.

  2. In the search field of the Actions tab, type Edit Settings JSON File.

  3. Add the following key to settings.json, use your custom path to the Conda executable:

    "conda.executable": "/opt/anaconda3/",

    The conda.executable key only works in global settings files.

    Setting the custom path to the Conda executable
Last modified: 23 August 2024