VS Code note recognizing python interpreter from venv - python

I keep all my projects in a folder called 'Projects' and have a venv in each project. I have the venv path setting in VS Code set to my Projects folder path, and when I use the Select Python Interpreter it recognizes the venv in my folder and says it is selected, however the venv is not being recognized because it won't recognize the packages I have imported.
enter image description here

So how I solved it was deleting the venv I created inside the project folder and creating a folder inside the project folder called venv where I installed the virtual environment. From there, the default paths in vscode were able to detect my venv and it was able to run. I'm not sure why it made a difference but it's working now!

Related

Virtualenv for django in VS Code not working, What am I doing wrong?

I've been following a tutorial on how to start using django and creating a virtual env on VS Code, but it doesn't work.. For what it shows in the tutorial, it's supposed to create a folder called ".vscode" with a json file inside called "settings.json" that contains the python path to the python interpreter..
But in my case, none of those files appear.. I THINK there might be sth wrong with the path where it creates the virtual env, but since I'm pretty new at this, I can hardly say..
This are the steps I followed:
C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON>cd DJANGO
C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO>mkdir storefront
C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO>cd storefront
C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>pipenv install django
C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>code .
C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>pipenv shell
(storefront-vT5YbUlq) C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>django-admin startproject storefront .
(storefront-vT5YbUlq) C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>pipenv --venv
** So the command prompt returns me this:
C:\Users\Usuario\.virtualenvs\storefront-vT5YbUlq
I'm supposed to copy that line to "Enter interpreter path" in VSCode, and after that it should create those vscode folder and json file.. but that doesn't happen, so I can't use the VS terminal to run the server
I'm going insane with this, I just can't understand where's the problem
I'd really appreciate if someone could help me with that
Thanks!
using terminal, mkdir to make a directory for your project
cd to your project folder/dir
type code . to open up vs code in this directory
Now you can use the integrated vs code terminal, please select the terminal, not the powershell
type pip3 freeze, it will show up all the installed packages and dependencies on global scope/system
but we gonna have a venv where we will install our necessary packages and dependencies
type python3 -m venv ./venv to create venv inside your current project folder, please ensure you are inside the folder before running this command
[if you are not using python 3, then the command will be python -m venv ./venv]
to actiavte environment,
on mac, run source ./venv/bin/activate
on windows, run .\venv\Scripts\activate.bat [if it doesn't work, try to put your absolute path]
you can check what is installed inside venv using pip freeze, you will see nothing inside the venv
Now you can install django inside venv for your project
inside vs code, press shift+cmd+p and search for Python: select interpreter and choose venv [enter interpreter path>find>project folder>venv>Scripts>python.exe]
then you can notice the settings.json file inside .vscode folder in your project
to deactivate the environment, just type deactivate
Still, if you don't see the .vscode folder inside your project folder, you can create it manually.
Create a folder and name it .vscode
inside the folder, create setting.json file and paste the following code
{
"python.analysis.extraPaths": [
"./YOUR_PROJECT_FOLDER_NAME",
]
}
for instance, system search for custom modules here in this extraPath if the module is not available in your system or user environment path.
You can try to follow this tutorial on the VSCode official docs.
You don't need to create the settings.json file, it was created by the VSCode, you can refer to here for the details.
And the interpreter-related information you can refer to here.

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 I Store Extra Info In My Python venv?

What I want to do is store a reference to the project location (my source files) in my venv.
The purpose for this is so I can automatically switch to that directory when I activate my venv.
I already have a script that managers my venvs. e.g. workonvenv xyz will activate the venv named xyz.
Why not add this logic to the script that manages the venvs?
A venv is not tightly coupled with a project, meaning venvs can be used for several projects and a project doesn't necessarily need a venv to work. Therefore there's no reference of a project in the venv.
What I recommend is to define a convention for project and venv names such as - project: "project-x" and venv: "project-x-venv" and then use your script to activate the venv and then cd into the project folder according to this convention.

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.

Visual Studio Code venv support

I'm using Python's venv and am unsure how to get VS Code to recognise the folder layout.
The interpreter is the system-level Python, so it doesn't see the project's Lib/, Scripts/, Include/, etc. and thus fails to find (import) the modules I've already added.
Is there a way to configure VS Code to work with venv projects and non-venv ones, or do I have to set python.pythonPath per-project?
Open a folder in the level above your venv directory. Then VS Code will discover the python.exe that lives inside /Scripts.
For example, if you created your venv in C:\MyProjects
C:\MyProjects> python -m venv MyEnv
Then in VS Code,
Open Folder... (Ctrl-K Ctrl-O)
Select C:\MyProjects
Command Palette... (Ctrl-Shift-P)
Python: Select Interpreter<Enter>
You should now see a dropdown list that includes .\MyEnv\Scripts\python.exe.
https://github.com/Microsoft/vscode-python/issues/2470#issuecomment-418459133

Categories