Automatically activating Python venv when using PowerShell in PyCharm - python

When using cmd.exe as the terminal application in PyCharm, the Python venv for the project gets automatically activated, so, I don't need to run venv\Scripts\activate. But when using PowerShell this doesn't happen. Is it possible to make this happen?

This is the option that has to be ticked to activate the Python virtual environment:
which is ticked by default and was ticket in my case. The reason why it wasn't working (and failing silently) was because I move the parent directory to a different location on my hard drive and that seemed to break the venv. Deleting it and re-creating it fixed the problem.

Related

vscode python intellisense doesn't work PROPERLY for some modules

the title explain's the problem, the module is tensorflow, recently i had the same issue with numpy also, but adding my site-packages path to vscode python->autoComplete->extraPaths fixed the problem in case of numpy, but it doesn't work for tensorflow!
now let me tell you what i mean by PROPERLY:
when i write a dot(.) after module name and then press CTRL + SPACE it shows nothing to me but when i type the first letter it starts showing attributes.
CTRL + SPACE after dot
after typing the first letter
it's not such a big deal but i just feel want it fixed, and i want to know the reason behind it to prevent such things again.
also my extraPaths in setting.json:
{"python.autoComplete.extraPaths": [
"/home/shayan/.local/lib/python3.10/site-packages/keras",
"/home/shayan/.local/lib/python3.10/site-packages/tensorflow/",
"/lib/python3.10/site-packages/keras",
"/home/shayan/.local/lib/python3.10/site-packages/keras",
"/usr/lib/python3.10/site-packages",
"/home/shayan/.local/lib/python3.10/site-packages",
"/home/shayan/.local/lib/python3.10/site-packages/numpy",
"/usr/lib/python3.10/site-packages/tensorflow",
"/home/shayan/.local/lib/python3.10/site-packages/tensorflow",
]
}
The reason for all this is probably because you have multiple python versions on your machine, which messes up the vscode environment.
It is recommended that you use a virtual environment, which is very helpful for using different python versions and installing different packages in different environments.
How to create and use a virtual environment:
Create a virtual environment named .venv using the following command in the vscode terminal
python -m venv .venv
Activate the virtual environment with the following command
.venv\scripts\activate
PS: Select the python interpreter in the virtual environment, and then create a new terminal, the virtual environment will be automatically activated.

VSCode does not recognize venv

When I create a new project and the virtual environment using the venv python package, VSCode doesn't recognize the new virtual environment. I follow the bellow instruction:
https://code.visualstudio.com/docs/python/environments
The command that I use in the VSCode integrated terminal is:
python -m venv .venv
The terminal that I use is PowerShell 7, But I tried the CMD terminal too.
After running this command, the .venv folder is created very well on the workspace and I checked its behavior on the terminal.
I tried conda package manager to create a venv and VSCode recognizes it. The problem is only with the venv Python package.
I also tried another Python version to create venv, But the problem still exists.
I read this question:
How can I set up a virtual environment for Python in Visual Studio Code?
I know how to add an environment manually, but I expect VSCode to recognize my environments automatically.
I added these lines to settings.json (Preferences):
"python.venvPath": "~/.venv",
"python.venvFolders": [
"~/.venv/Scripts"
]
I activated the venv manually using VSCode integrated terminal.
The problem still isn't solved.
This is a screenshot of my problem:
I know how to add an environment manually, but I want it to be automatic.
I tried VSCode on another PC, and it worked; It doesn't need anything to do except the presence of venv.
VSCode Version: 1.58.0
Python Extension Version: v2021.6.944021595
The "python.venvPath" should be set to the parent folder of the .venv folder.
Virtual environments located in the folder identified by the
python.venvPath setting (see General settings), which can contain
multiple virtual environments. The extension looks for virtual
environments in the first-level subfolders of venvPath.
But, you need not set that. The Python extension should find the venv environment, as it is just located under your workspace folder.
Virtual environments located directly under the workspace (project)
folder.
I tried it locally, it does not work too. It will only show the cached environment path, and the "python.venvPath" does not work either.
And there are some changes of the interpreter storage:
A VSCode internal storage is introduced which will now store the
interpreter settings in the workspace & workspace folder scope.
You can refer to here for more details.
So, it's recommended to select the environment path manually for now.
After writing this command in VS CodeTerminal
python -m venv env
You can simply use
.\env\Scripts\Activate
In the same directory path where you have created the virtual enviroment.
For some reason VSCode had uninstalled all my Python extensions including the main Python extension. After reinstalling them it was able to detect my virtualenvs again.
So I was having this same issue and I resolved it by,
1. Open Settings in VSCode.
2. Search for "python.terminal.activateEnvironment"
3. If unchecked/unticked, then check/tick for both "User" and "Workspace".
The problem is not with VSCode but the workspace settings in which I was working on.
Just changed that and Voila works like a charm.

Can't run Venv in VsCode

For the last 3 days, I have been trying to set up virtual Env on Vs Code for python with some luck but I have a few questions that I cant seem to find the answer to.
Does Vs Code have to run in WSL for me to use venv?
When I install venv on my device it doesn't seem to install a Scripts folder inside the vevn folder. Is this out dated information or am I installing it incorrectly. I am installing onto Documents folder inside my D: drive using python3 - m venv venv. The folder does install and does run in WSL mode but I am trying to run it in clear VsCode so I can use other add-ons such as AREPL that doesn't seem to like being ran in WSL.
For extra context I have oh-my-ZSH set up and using the ubuntu command line on my windows device. Any information will be helpful at this point because I am losing my mind.
venv folder in side D: drive
result
If you have the python extension installed you should be able to select your python interpreter at the bottom.
You should then be able to select the appropriate path
Run Set-ExecutionPolicy Unrestricted -scope process before activating virtual environment.
All the best
You don't have to create a virtual environment under WSL, it will work anywhere. But the reason you don't have a Scripts/ directory is because (I bet) you're running VS Code with git bash and that makes Python think you're running under Unix. In that case it creates a bin/ directory. That will also confuse VS Code because the extension thinks you're running under Windows.
I would either create a virtual environment using a Windows terminal like PowerShell or Command Prompt or use WSL2.

Python not showing existing venv in interpreter select

I have a shared flask web project I am working on with 2 other developers, one of the developers initialized the venv on his pc, uploaded his project structure to github from where I cloned his repo.
Now I when I start vscode and open the project folder, python does not auto detect the venv and asks if it should set is as the interpreter, the only option I have is the default system wide python install, and not the venv python interpreter.
I tried adding it to the list by using the command python:select interpreter and then finding the python.exe inside the venv/scripts folder, but this does not work and vscode still asks for a interpreter.
I also tried manually adding it inside of my workspace settings.json file like so
"python.pythonPath": "C:\\laragon\\www\\Proftaak\\venv\\Scripts\\python.exe"/
But vscode also gives an error on this saying the interpreter is not valid.
How would I fix this?
This is not expected to work as virtual environments are not designed or meant to be movable. They are meant to be created on each machine you need a virtual environment on. As such, I suspect that the virtual environment does not work outside of VS Code which could prevent it from selecting it as a possible working environment.

Why my VSCode's interpreter is not working in Mac?

I have VSCode and I am trying to set up a virtual environment. First, I created all the venv properly. When I run in the Terminal is working and activated. However, it is not showing up in the VSCode. Make the matter worse, I can not change my interpreter at all. At first, I had python 2.7 and I could not change into python3 even though everything is set up properly. I fixed that problem with this setting "code-runner.executorMap.python": "python3 -u". And how I cannot change to another interpreter without the delete the setting. I am not sure what to do. Please help.
Thanks
Others have reported setting up virtual environments using VSCode. I suggest you set up your virtual environment using the terminal and activating the environment before starting VSCode. If you are using Anaconda Python this source will instruct you on setting up a virtual environment. For non-anaconda python, there are many pages for doing the same
To change the interpreter in VSCode, go to the View menu and select the Command Palette. When the command palette opens type python in the text box. This will cause commands with python in its name to be displayed. The first command (on my machine) is Python: Select interpreter. Select this and hit return. You will be given a list of python interpreters to choose from.

Categories