0

I am running Pycharm 2019.3 on Linux Ubuntu 18.04 LTS. I have created a virtual environment for my Pycharm project from within the IDE. I am able to access the packages from the virtual environment when I run programs from within the IDE. However, if I activate the virtual environment created from within Pycharm from the terminal (using source venv/bin/activate, resulting in the (venv) prefix on the command line), the packages I added to the virtual environment can no longer be found. Why is this? The (venv) prefix makes me think that I have successfully activated the virtual environment. What can I do to use this virtual environment outside of Pycharm?

rhz
  • 570
  • 6
  • 16

1 Answers1

0

I think this is because pycharm has created a virtual environment in a different location. I think you can check both the locations with echo $VIRTUAL_ENV and check if they are the same.

Anurag
  • 432
  • 3
  • 22
  • You are right! I had previously changed the name of the project and its root directory via the IDE's refactor feature. The new virtual environment now appears in the IDE, but the old one appears when $VIRTUAL_ENV is printed. How do I fix this? – rhz Dec 10 '19 at 05:28
  • you can delete one virtual environment with `rm /path/to/your_venv` and use another one with `venv /path/to/your_other_venv` – Anurag Dec 10 '19 at 05:46
  • The value of $VIRTUAL_ENV points to a virtual environment in a directory that no longer exists (under the old, pre-refactoring project directory which was moved in Pycharm's refactor). There's nothing to delete. It seems that the refactor wasn't 100% clean, since the old path was retained somewhere (despite everything working fine with the correct path in Pycharm). – rhz Dec 10 '19 at 06:18
  • you can change the location of the virtual environment in pycharm setthings/preferences. For more details: https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html – Anurag Dec 10 '19 at 06:37
  • Perhaps, I didn't explain myself well. Pycharm is already pointing to the correct virtual environment. The value of the Project Interpreter field is: ~/PycharmProjects/new_proj/venv/bin/python. When, from the terminal, I issue the command: source ~/PycharmProjects/new_proj/venv/bin/activate, followed by echo $VIRTUAL_ENV, I get /home/my_name/PycharmProjects/old_proj/venv. old_proj no longer exists, but it was the name of the project directory (and the name of the project) prior to a refactor I did. How do I get the manually activated virtual environment to point to the correct location? – rhz Dec 10 '19 at 18:15
  • I looked inside ~/PycharmProjects/new_proj/venv/bin/activate, and I can see that it sets VIRTUAL_ENV to /home/my_name/PycharmProjects/old_proj/venv. Seems like I could hand edit this file such that VIRTUAL_ENV points to the correct location a la: https://stackoverflow.com/questions/6628476/renaming-a-virtualenv-folder-without-breaking-it It appears that Pycharm does not make direct use of this variable? – rhz Dec 10 '19 at 18:28