0

I work on pycharm project and I have a virtual environment (venv) with Python version 3.6.

I need to change it to 3.7 but I don't want to have to create a new environment and install all the packages from the beginning. Is there a way to do that? Just change the python version while keeping all the packages?

Cranjis
  • 1,590
  • 8
  • 31
  • 64
  • I'm afraid that's not possible. Your Python executable is what you have inside your virtual environment. To change that you have to remove your venv and create it again using a different version of Python. – Omid Shojaee Jun 22 '21 at 18:05
  • @OmidShojaee Thanks, is there a way to easily install all packages from the python3.6 venv once I created the venv with Python3.7? – Cranjis Jun 22 '21 at 19:35
  • @okuoub [write a requirement file](https://stackoverflow.com/q/7225900) or a shell script to execute the commands for you. – bad_coder Jun 22 '21 at 20:12
  • @bad_coder Not a dup — the OP doesn't have a local directory with packages. A venv with installed packages is not suitable. – phd Jun 22 '21 at 21:18
  • @phd the question is `venv` specific (otherwise it would lack focus because any number of package managers could be recommended - but that's not what is asked). You say "a venv isn't suitable" but in fact it works and that's what the OP is asking about. – bad_coder Jun 22 '21 at 21:59
  • @bad_coder I meant old venv for Python 3.6 is not suitable to install packages for Python 3.7. The TS should either install from PyPI or download packages from PyPI and install from that directory, not from the old venv. – phd Jun 22 '21 at 22:41

1 Answers1

2

Unfortunately what you are looking for is not possible.

Your solution is to create a requirements.txt file from your current environment, create a new one with Python 3.7 and re-install the packages.

To create the file, issue this while your current environment is active:

pip freeze > requirements.txt

And then, this one when the new environment is installed and active:

pip install -r /path/to/requirements.txt

P.S: requirements.txt is just a name everyone uses. It's not written in stone. Use any filename you want.

Omid Shojaee
  • 333
  • 1
  • 4
  • 20