How come VS Code activates the virtualenv automatically? - python

I am trying to learn more about Python in my spare time. I am a .net developer. I have downloaded a simple private open source project. The developer of the repository advised me to clone the repository and then run the project:
source .env/bin/activate
python3.6 ./project/main.py -c config.json
It works as expected. I am now thinking about debugging so that I can step through the code and debug it. Therefore I have downloaded Visual Studio Code and then installed the Python extension. I then start debugging and everything still works as expected. I opened the Visual Studio Code terminal and I see that Visual Studio Code runs the following:
bert#bertvb:~/myapp$ source /home/bert/myapp/.env/bin/activate
(.env) bert#bertvb:~/myapp$ /usr/bin/env /home/bert/myapp/.env/bin/python /home/bert/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/launcher 43369 -- /home/bert/myapp/myapp/main.py -c config.json
How does Visual studio Code know to run the first line i.e. to enter the virtual environment? I was advised that this would have to be run manually

Visual Studio Code automatically scans your workspace folder for any virtual environments. If it finds one, it will automatically enable it and select the Python interpreter installed in this virtual environment. If you want, you can disable this behaviour by setting the python.terminal.activateEnvironment option to false.
For more information, see the "Using Python environments in VS Code" documentation page, specifically the section "Where the extension looks for environments".

Related

Debug a c++ python 3.10 extension, venvlauncher.pdb missing

I followed Microsoft excellent tutorial to create a Python extension in c++. Everything works fine, I can compile, run and debug the code (both the Python and the C++) in Visual Studio 2022.
However, the issue is that I want do this within a venv, this was possible with Python 3.7.0 but now when I create a venv with Python3.10-64 I can't debug the C++ part.
I have included the include and libs of the "global Python" in the Visual Studio 2022. Do I miss something when I create the venv with c:\python310-64\python -m venv venv? There seems to be a lot less in the Script folder now with Python3.10 compared to with Python3.7.
is there anything that I can change within Visual Studio 2022 to hit the C++ breakpoints when I run Python from venv created with Python3.10?
When I look at what modules that are loaded when I start the debugging from my venv, and right click to find the symbols for Python.exe it looks like this:
.
Compared to this long list when I start the debugging with the "global" Python installation:
Woho! I finally figured it out. The venv needs to be created with --symlinks like this C:\Python310-64\python.exe -m venv venv --symlinks. You need to run the command as administrator to get it to work!

Adding path of downloaded Python to Visual Studio Code

Just gone through the process of setting up python and all the installs using cmd and now i want to use
brownie bake
To clone some code and it gives errors as it cant locate brownie... I've set environmental variables for the path (that's how I got it to work in cmd)... I've installed the python extension and chose the python.exe file in my new install in the compiler by clicking the bottom left... How can I make visual studios Code use only the python and all library's I installed?
if i run
python
in the terminal it opens up windows store for python install but i can run my version by using
py
First, Disable the python command to open the windows store:
Search Manage app execution aliases and unselect App Installer of Python:
Second, You need to check which python you have installed your packages to:
And make sure in the VSCode you have selected the right interpreter(In the following picture, they are different).

Is a different version of python being used when the code is run in visual studio Code?

I am trying to learn more about Python in my spare time. I am a .net developer. I have downloaded a simple private open source project. The developer of the repository advised me to clone the repository and then run the project:
source .env/bin/activate
python3.6 ./project/main.py -c config.json
It works as expected. I am now thinking about debugging so that I can step through the code and debug it. Therefore I have downloaded Visual Studio Code and then installed the Python extension. I then start debugging and everything still works as expected. I opened the Visual Studio Code terminal and I see that Visual Studio Code runs the following:
bert#bertvb:~/myapp$ source /home/bert/myapp/.env/bin/activate
(.env) bert#bertvb:~/myapp$ /usr/bin/env /home/bert/myapp/.env/bin/python /home/bert/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/launcher 43369 -- /home/bert/myapp/myapp/main.py -c config.json
however, when debugging in visual studio code; there are exceptions being thrown e.g. 'JSONDecodeError' object is not subscriptable and File not found error etc.

I can't run python on visual studio code

Image case for the problem
I was following the Visual Studio Code tutorial for python here
https://code.visualstudio.com/docs/python/python-tutorial
I downloaded Python, installed the python extension in Visual Studio Code and set the python interpreter to python3.8 . I also checked the workspace settings to see if it had given the correct path. Then, I wrote a simple hello world program but this is the error it is giving me.
This is the python path it has in the settings.
C:\Users\Dell\AppData\Local\Microsoft\WindowsApps\python.exe
Program:
print("Hello World")
Error:
C:/Users/Dell/AppData/Local/Microsoft/WindowsApps/python.exe c:/Users/Dell/hello/hello.py
-bash: C:/Users/Dell/AppData/Local/Microsoft/WindowsApps/python.exe: No such file or directory
I tried uninstalling and installing all the extensions again but it didn't work. I also uninstalled and installed all python related things on my computer. That didn't work either. What should I do?
reinstall python and this time, add python to the PATH (there is an option on the installation screen. Check it as it isn't checked by default). Then try running vs
Your environment path is not configured properly reinstalling python will fix this make sure you install python from actual application and not from vscode.other solution is that you can set the enviornment path manually just go to the environment variables> under user variable set the path to your python.exe the above mentioned location seems unlikely it should contain main python folder such as Python37/....
I will suggest you follow the link mentioned in this blog post to have python installed and working with Visual Studio Code. https://techdirectarchive.com/2023/01/17/getting-started-with-python-automation-in-windows-with-visual-studio-code/
Microsoft recommends installing python from the Microsoft Store. since installing from the Microsoft Store uses the basic Python3 interpreter, and handles the set-up of your PATH settings for the current user (avoiding the need for admin access), in addition to providing automatic updates.

Configuring Python in Visual Studio Code on Windows 10

I was working with Visual Studio Code and Python 3.7 installed in the default installation directory (\users\user\appdata\roaming), but when I updated my Python installation to the last version, "3.9.1", and after deleting all previous versions, I decided to install it in another directory (C:\Python), my Visual Studio Code does recognize my Python installation as you can see below:
But when running Visual Studio Code again, it tells me that Pylint isn't installed, logical since I deleted everything from the previous installation.
The problem I'm facing right now is that when I install "pylint" it keeps installing in the default Python directory (\users\user\appdata\roaming) and telling me to add it to the "environment variables".
How can I configure Visual Studio Code to install all libraries/modules in my new Python directory (C:\Python)?
An environment variable tells your system where the python.exe file is located. Pylint doesn't know where python.exe is. Use these steps:
Right-clicking This PC and going to Properties
Clicking on the Advanced system settings in the menu on the left
Clicking on the Environment Variables button on the bottom right
In the System variables section, select the Path variable and click on Edit. The next screen will show all the directories that are currently a part of the PATH variable
Clicking on New and entering Python’s install directory

Categories

Resources