2

If I start a new python project, pycharm sets the Python 3.9 (venv) as default interpreter. With this, I need always reinstall packages, that were already installed, so I changed to Python 3.9 interpreter, but I don't know what is the real difference, and wich one should I use.

Python 3.9 and Python 3.9(venv) option

L4r4TW
  • 45
  • 7
  • 2
    you need to understand what virtual environments are if you are not aware of them, https://docs.python.org/3/tutorial/venv.html – python_user Jan 02 '21 at 14:33
  • 3
    Does this answer your question? [What is a virtualenv, and why should I use one?](https://stackoverflow.com/questions/41972261/what-is-a-virtualenv-and-why-should-i-use-one) – python_user Jan 02 '21 at 14:36
  • So if I create a new project in pycharm, it will aslo create a virtual environment, and with the "python 3.9(venv)" interpreter I can use only the packages inside that venv, but if I cahnge to "python 3.9" interpreter, it will use the packages inside the root python folder. – L4r4TW Jan 02 '21 at 15:06
  • 1
    I have not used PyCharm, but the TL;DR of using venvs is that you can install any version of any library in that specific venv without affecting your global python, so for example you can have numpy 1.8.0 in a venv and numpy 1.8.2 in a different venv, it is not possible to have different versions of the same library if you install in the root python folder – python_user Jan 02 '21 at 15:14

1 Answers1

2

The difference is that, unlike the base interpreter, which is not context dependent, the venv (virtualenv) interpreter serves a specific project.

The advantage of using a virtual environment is that you can maintain different packages and package versions depending on the requirements and dependencies of each project separately.

In order to avoid reinstalling the same packages each time you can:

  1. Inherit global packages. By default the venv interpreter comes with the minimal packages installed, but it is possible to inherit the global packages. I see you are using PyCharm, so you can follow these instructions under "If New environment is selected:", section 3.

    You can also achieve this using the virtualenv package directly by adding "--system-site-packages" to the creation command (e.g. virtualenv venv --system-site-packages).

  2. Create a requirements.txt file. Pip can install packages using a file the specifies the packages (and optionally package versions) you would like to install. You can run:
    pip install -r /path/to/requirements.txt Where the file is usually located at the root folder of the project.

    In order to create such a file from an existing interpreter you can use the command

    pip freeze > /path/to/requirements.txt

I personally prefer the second option.

Zgos
  • 44
  • 4