How activate virtualenv from pycharm terminal

Answered

My project interpreter is setup to use a virtual environment, which was created by pycharm while it was creating the the project.

While I used to open project, pycharm used to provide me the terminal with virtual-environment is activated.

But I executed "deactivate" command from terminal provided by pycharm. 

How can I activate the virtual-env from the terminal ?

 

1
16 comments

If Activate virtualenv is checked under Settings/Preferences | Tools | Terminal, then it should be enough to just close the terminal tab and open a new one.

4
Avatar
Permanently deleted user

I am sure I have done it. But it still doesn't work. I still have to activate the environment myself. How should I do it?

0
Avatar
Permanently deleted user

Same trouble at Windows 10

0

Same here. I can see the (venv) prompt, but I still need to manually run "source venv/bin/activate" 

0

>I can see the (venv) prompt

What is the output of `which python` command from the terminal with (venv) prompt?

0

Andrey Resler Thanks for the quick reply! I have found the issue on https://youtrack.jetbrains.com/issue/IDEA-209282

The PATH is not set correctly for some reason, so `which python` gives the path to the system python. 

0
Avatar
Permanently deleted user

I have the same problem in Windows 10, leading as a side effect that cmd in Pycharm has its own virtual environment. How would I fix this?

0

Maik Himstedt

Please report it to https://youtrack.jetbrains.com/issues/PY and attach idea.log file from Help | Show Log in... along with a screenshot of the terminal settings in File | Settings | Tools | Terminal.

0

What solved it for me was from the issue above. 

```

Here is some workaround I found in case anyone else encounters the same issue:

  • Disable "Preferences | Tools | Terminal | Shell integration"

  • Use bash

```

2

I changed the value of the option:
File \ Settings \ Tools \ Terminal \ Shel path
from
C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
to
C:\WINDOWS\system32\cmd.exe
and restarted Pycharm.
The virtual environment started automatically.

19

Trotsky36 thanks man!! was searching this only

 

0

I think this is related to the command that I usually input before I enable venv:

Set-ExecutionPolicy Unrestricted -Scope Process

 Does anybody know how to start powershell with this command passed as a command-line argument?

My theory is: CMD doesn't have this mechanism of protecting the user from programs executing scripts, but Powershell does. It's possible that it's the culprit.

 

EDIT\\ I managed to make it work. It was indeed related to the execution-policy/scope theory that I had yesterday (at least for me). I opened powershell away from IntelliJ and wrote this command:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser 

I then closed powershell's window, closed the terminal tab (usually called 'Local') in IntelliJ, and clicked on the terminal button in IntelliJ again.

And voila:

P.S: It would be great if someone could shed some light on the potential security repercussions of setting the execution policy to unrestricted.

1

Robert Cannon 
`Disable "Preferences | Tools | Terminal | Shell integration"` works for me. Thanks!

0

In the Preferences | Tools | Terminal Settings there are system environmental variables. After deactivation of those, terminal started within .venv. 

0

I had the same problem that my virtual environment was no longer being activated. This happened after I had moved the project to a different location on the local drive. I finally found the “Python Interpreter” setting in

`Settings | Project: <project_name> | Python Interpreter`

which was specified with an absolute path, so was now invalid. All I needed to do was `Add Interpreter` and select the new location of the python.exe and restart PyCharm and it worked again.

I feel like there should have been an error telling me that the specified python interpreter was missing/invalid, and if there was one, it should have been more obvious/in my face :)

1

Please sign in to leave a comment.