The virtual environment is not activated in the pycharm terminal - python

I use pycharm, but my power shell does not recognize the virtual environment? What do you think I should do so that he can recognize the virtual environment?
And that this happened after I moved my django project folder to another folder
terminal setting :
And that my virtual environment is recognized by pycharm and the application runs, but when testing the terminal, it does not recognize my virtual environment.
when i run Get-Command python Instead of the path of the virtual environment, it takes the main path of Python

Related

How do I ensure python project in VSCODE is using virutal envirnoment and not defaulting to global?

Having an issue when using flask and flask-wtf. The issue is that for some reason vscode is defaulting to global rather than the newly created virtual environment. In vscode I will click open folder, create the folder and open it in vscode. Then I'll write my code out and then open terminal in vscode and write: python -m venv my_venv, then on the lower left-hand side I will select it as my virtual environment. I then re-open the terminal, write: pip -V and the pip it is directed to is the folder directory with my_venv in the path name.
Then I pip install flask and flask-wtf, check that they are installed with pip list. Everything looks good until I run and vscode says: ModuleNotFoundError: No module named 'flask-wtf'. But when I go to my global environment and where flask is installed too it runs fine. Something is happening to where the python code is recognizing the original flask and not the one in my virtual environment. How do I ensure that the version of flask is the one in the virtual environment and not others installed on my machine.
Thanks
If you are in your terminal in vscode, it should append your command line with your virtual environment name.
So, usually it would show
PS \path
But if your environment is running, it will show
(venv name) PS \path
If it is now showing, be sure to reactivate your virtual environment from your project directory

How to use the Python debugger in venv in VScode on Windows?

OS:Windows10Pro
IDE:VScode Ver1.59.0
Python:Ver 3.9.6
I created a virtual environment with venv and selected python.exe on the virtual environment with "select interpreter".
Even if I specify a breakpoint on a simple script and execute debug (F5), the breakpoint will be ignored and executed.
In Ubuntu 20.04 on VMware, I debugged in the same way using VScode Ver1.59.0, so it will stop at the breakpoint.
In both Windows and Ubuntu, I didn't specify the path in launch.json or setting.json.
On Windows, I specified the python path in launch.json, but the debugger doesn't work.
Is this a bug specific to the Windows environment? Or am I forgetting to set something?
1.python -m venv [ENV_NAME]
2.Open Folder [ENV_NAME] on VScode
3.Open Terminal
4../Script/acitivate
5.create python script
a=0
print(a) #<-set berak point
6.select interpreter -> ./Script/python.exe
7.press F5(run dubug)
I didn't understand that VScode on Windows had to create an ENV folder directly under the workspace folder.
Curiously, on Ubuntu, the debugger works even if the ENV folder is a workspace folder.
1.Create a project folder
2.Open project folder on VScode
3.Open Terminal
4.cd project folder
5.create venv (python -m venv [ENV_NAME])
6../[ENV_NAME]/Script/acitivate
7.create python script
8.select interpreter -> ./[ENV_NAME]/Script/python.exe
9.press F5(run dubug)

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.

The virtual environment is made in my user s folder

No matter what folder am I'm, command 'pipenv shell' creat virtualenvironment named by my username, doesn't create virtualenvironment named by my project(folder's name). Could you please help me with that?
According to your description, I used "pipenv shell" to create a virtual environment on my computer. When it was originally in the global environment python3.8.3, it was named ".venv" by default:
It is recommended that you exit the original virtual environment before using "pipenv shell" to create a virtual environment. In addition, as 'cizario' said, we often use the command "python -m venv .venv_name" in VSCode to create a virtual environment and it can still be in this virtual environment automatically after refreshing the console:
Reference: Virtual environment in VSCode.

what does activating virtualenv in python exactly mean and what it does?

When we want to do anything with our python virtual environment in terminal/command prompt/shell, we have to activate it by navigating to the scripts folder. But, what does this activation do?
I can access the contents of the virtualenv folder in file explorer without activating it.
At its core, the main purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has.
More interesting are the activate scripts in the bin directory. These scripts are used to set up your shell to use the environment’s Python executable and its site-packages by default.
After activating the environment, we’re now getting a different path for the python executable because, in an active environment, the $PATH environment variable is slightly modified.

Categories