How to set several paths in VScodes Python extension - python

I want to set 2 interpreter Paths in VScode(Windows) for the Python extension.
I don't know how and I don't want to use .env files. I want to add Python 2.7 and 3.8
VScode settings.json:
...
"python.languageServer": "Pylance",
"python.defaultInterpreterPath": "C:\\Users\\{myuser}\\AppData\\Local\\Programs\\Python\\Python38-32\\python.exe",
"python.showStartPage": false,
...
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}
]
}
I know that there is "python.autoComplete.extraPaths" but it didn't work.

The python extension can detect the python interpreter automatically. It will automatically look for interpreters in the following locations:
1. Standard install paths such as /usr/local/bin, /usr/sbin, /sbin, c:\\python27, c:\\python36, etc.
2. Virtual environments located directly under the workspace (project) folder.
3. 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
and so on... You can refer to the official docs for more information.
You can also manually specify an interpreter if Visual Studio Code does not locate it automatically. like this:

Related

Tests not discovered because of dependencies on local modules out of the project

With the testing extension I get a ModuleNotFoundError. I have a workspace with 3 repositories. With a structure like this. The main repo uses the two other repo's, when publishing we use wheels, but for the unittests I do not want to install the wheels all the time.
main_repo
app
tests
sub_repo1
sub_repo2
When debugging the app already knows that sub_repo1 and sub_repo2 exist. But the testing extension does not understand this. Because I have added it to the launch.json as:
"configurations": [ { "name": "VIKTOR Debug", "type": "python", "request": "launch", "module": "my_debugging", "justMyCode": true, "env": { "PYTHONPATH": "../sub_repo1;../sub_repo2" } },
And to the settings.json as:
"python.autoComplete.extraPaths": [ "C:\\DevOps\\Pythonproject\\sub_repo1", "C:\\DevOps\\Pythonproject\\sub_repo2" ], "python.analysis.extraPaths": [ "C:\\DevOps\\Pythonproject\\sub_repo1", "C:\\DevOps\\Pythonproject\\sub_repo2" ],
But the testing extension in VSCode does not listen to this.
Is there another way to fix this in VSCode or should this be a feature request? Because it seems unnecessary that the debugging knows my local module, but the testing extension does not.
That the testing extension in VSCode would know where my local modules are located just like the debugging does.

How to set environment variables with bat file before starting python debugging in vscode

I'm doing simulations with Adams Car 2020 and Python. I use the internal Adams Python functionality. In this internal python environment I don't have matplotlib available for example. Therefore I've created a virtual python environment which has extra packages which I start from Adams Car Python as a subprocess.
Before we start Adams Car we set some of the environment variables with a Batch file, let's say set_env.bat. I need these environment variables as well when I start python in the virtual environment. I can do this with a batch file where I first call set_env.bat and then call the virtual env Python:
start_ext_python.bat:
call set_env.bat
call <path_to_python_virtual_env> %1
where the first argument is the path to the python script I want to execute.
I would like to use Vscode debugging and call set_env.bat. Therefore I made a task which I refer to in launch.json:
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "init_acar",
"type": "shell",
"command": "C:\\User\\lj012145\\Git\\Adams\\source_a2cm\\bat\\ADAMS_Set_Environment.bat"
}
]
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Test adams external file",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"preLaunchTask": "init_acar",
"args": [],
},
]
}
This runs the bat file first but the environment variables are not set anymore when I stop the debugger in my python script.
I could also create a set_env_vscode.bat which writes to a .env file which I refer to then but I'm not sure if that is a good approach..
Take a look at this Is there any way to set environment variables in Visual Studio Code?.
But as far as I can tell, the simplest way is to:
Open console.
call the .bat file
Open vscode with: code .

How to run python from different drive using Visual Studio Code

My OS is Windows 10.
I have test.py file in E drive.
I installed Python 3.7.2 and Visual Studio Code in C drive.
I set system path for Python 3.7.2 so I could run python on CMD on both of the drives.
I also set python.pythonPath in setting.json file in .vscode under my work folder.
So python.pythonPath is same as the system path.
but I can't run that test.py file on VScode.
It shows python 3.7.2 file's path and says "No such file or directory".
Is there a way to run python file on different drive using VScode?
You can use debugging to run specific files in a reusable way.
https://code.visualstudio.com/docs/python/debugging
It will create a launch.json file with contents similar to this. You can add additional
properties. VSCode is good about helping you create and edit the launch.json file.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "RunPython",
"type": "python",
"request": "launch",
// run a file by specifying a full path
"program": "E:/FullPathTo/FileA.py",
// OR run the file currently open in VSCode
// // "program": "${file}",
// OR run a file relative to the current folder you have open
// // "program": "${workspaceFolder}/folder/FileB.py"
"console": "integratedTerminal",
"args": ['InputArg1','InputArg2']
}
]
}
If you've already set Python as an environment variable, you can simply route to the directory where the file is present (in this case E: drive) and run the command:
python test.py
I installed Code Runner Extension and it followed my system path of python 3.7.2 automatically.
Now I can simply run my code with this short cut "Crtl+Alt+N" instead of entering "python test.py" every time on the terminal.

VScode debugger cannot import modules from site-packages

I have C:\Users\user\AppData\Roaming\Python\Python37\site-packages directory in sys.path
And while I run code via cmd it works fine.
However, when i try run it via vscode debugger, I get this:
No module named request
So, how can I fix this?
According to your description, it is recommended that you could try the following methods:
Check the python interpreter for the current VSCode.
When running python files in the cmd window, the system uses the python set by the environment variable.
About it can run in cmd, but not in VSCode.This will happen if the Python interpreter used in VSCode is different from the cmd. Reference:python-interpreter.
Try using pip to install the required module again. Use 'pip --version'(Linux and Mac:'pip -v') at the terminal to check whether the version of pip comes from the current interpreter.Use 'pip install <modules name>' to install it. Then, check whether the module is in the list by 'pip list'.
Attempt to reload VSCode.
Above, I could import the required modules from similar locations. My environment:python:3.8.3; VSCode:1.47.2; Win10.
Update:
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}

How to configure VS Code to be able to step into a shared library (.so) that is loaded when debugging a Python script?

Using gdb from the command line I'm able to break when the shared library is loaded. How can I get the same behavior in VS Code knowing that I have the source code of the shared library?
For me it works somehow.
Here's my setup:
Ubuntu 18.04, debugging a C++ shared library I load from Python3 (more specifically - via Cython, but IIRC it worked equally well when loading a .so through ctypes, also I remember it working when debugging a pure C lib in a similar setup)
in VSCode I have a project that compiles into that .so
there I put a bunch of breakpoints
I created a launch configuration (text below)
also, I've compiled the .so with debugging information
here's my launch.json (for the most part, it's boilerplate, I only filled in the "program" and "args" parts and set up PYTHONPATH environment var).
note: it seems to be important to have "stopAtEntry:false" (which it is by default), otherwise VSCode tries to locate an entry .c file or something.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch 1123",
"type": "cppdbg",
"request": "launch",
"program": "/home/sergey/anaconda3/bin/python",
"args": [
"/storage/projects/cython-vst-loader/cython_vst_loader/test_load_plugin.py"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "PYTHONPATH",
"value": "/storage/projects/cython-vst-loader"
}
],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
anyway, in this setup I see my VSCode showing the execution stopping on my breakpoints
Unfortunately there isn't a way to flow from Python code into C code for debugging purposes (only Visual Studio has that ability to my knowledge).
Thank you #user1312695, your method works for me!
I was able to step into pybullet.c now! So let me take this as an example.
https://github.com/bulletphysics/bullet3
I want to install the debuggable version pybullet package into a conda environment and use VScode to start my debugging, here is what I did:
(0) create a new conda environment called debug_pybullet.
(1) modify cmake/FindPythonLibs.cmake
FindPythonLibs.cmake can't recognize conda environments, so after it found the wrong _PYTHON_EXECUTABLE, I need to manually set the path at around line 143:
set(_PYTHON_EXECUTABLE /home/MyName/anaconda3/envs/debug_pybullet/bin/python)
(2) modify CMakeLists.txt
I need to manually add the definitions which are included in file build_cmake_pybullet_double.sh, except the definition of CMAKE_BUILD_TYPE=Release (I prefer to let VSCode control this definition).
also I manually set PYTHON_SITE_PACKAGES at around line 93:
set(PYTHON_SITE_PACKAGES /home/MyName/anaconda3/envs/debug_pybullet/lib/python3.6/site-packages)
(3) Create launch.json in VScode.
Here is my version of launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/MyName/anaconda3/envs/debug_pybullet/bin/python",
"args": [
"/home/MyName/<path_to_python_file>/main.py"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
}
]
}
Since I manually set the path, I don't need the environments and setupCommands here.
(4) In VScode, build all.
(5) Install the debuggable package:
$ source activate pybullet_debug
(pybullet_debug)$ pip install -e .
(6) Set the breakpoints in pybullet.c.
(7) Press F5, Run the python, and here we are!
A screenshot attached.

Categories