Can't get VSCode/Python debugger to find my project modules - python

I have a project and am trying to debug my main.py. I am really confused why I am getting the following error from the imports at the top of my file (only) when running the debugger:
Exception has occurred: ModuleNotFoundError
No module named 'bbb'
File "/Users/maxepstein/myproject/bbb/train/__main__.py", line 8, in <module>
from bbb.mysubfolder.myfile import myfunction
My project folder structure, as shown by these print statements (as shown by the debugger) confirms my 'bbb' module exists, and has an __init__.py:
import os
print(os.getcwd())
print(os.listdir())
print(os.listdir('bbb'))
/Users/maxepstein/myproject
['requirements.txt', 'bbb', 'resources', '__init__.py', 'readme.md', 'results', '.gitignore', '.git', '.vscode', 'bbenv']
['config', 'tests', '__init__.py', 'utils', 'predict', 'train']
I'm trying to debug as "debug current file - integrated terminal", below is the applicable debug settings from my debug settings.json. After searching online, I really thought adding "cwd": "/Users/maxepstein/myproject" below would be my solution but it hasn't helped.
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "/Users/maxepstein/myproject"
}

A simple workaround to the bug mentioned by #BrettCannon is to add the following env entry to the launch.json configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": { "PYTHONPATH": "${workspaceRoot}"}
}
]
}

Had the same problem when importing from a nested directory, and fixed it by appending to the env variable PYTHONPATH:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH":"${PYTHONPATH}:/home/maxepstein/myproject/"
}
}
]
}

When I am debugging a Python module in VS Code I use the Module debug configuration instead of the Current File one. For you it might look like this:
{
"name" : "Python: Module",
"type" : "python",
"request": "launch",
"module": "bbb",
"args": []
}
See the documentation https://code.visualstudio.com/docs/python/debugging
Also, in VS Code, these steps will auto-populate these settings for you:
Debug -> Add Configuration -> Python: Module

In my case, I quickly fixed it selecting the right interpreter:

You can use the current file debug configuration. In the file you're debugging that's importing the modules add the full path to the modules you're trying to import to your system path.
sys.path.append('/Users/my_repos/hw/assignment')
import src.network as network
The module here is src, located in the assignment directory.

I run the debugger from VS Code.
My structure in VS code:
myproject
+vscode
+---launch.json
|
+src
+---test/
+------MainTest.py
+---Main.py
the launch.json that saved me:
{
// 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: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"env": {"PYTHONPATH": "${workspaceRoot}:src"},
"console": "integratedTerminal"
}
]
}

Related

Can't Debugg using VS Code because it doesnt Find a local file

Currently when I debbuging moves to my_proyect and run
/home/my_user/my_company/my_proyect/my_folder/my_code.py
However, I getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'data/my_data.csv'
I trying to change the cwd variable in launch.json inside .vscode as follows:
{
// 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: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {
"ENVIROMENT_EXECUTION": "local",
"LOGGING_LEVEL": "info",
"ENVIROMENT_STAGE": "develop",
"ENVIROMENT_DEBUG": "yes"
},
"cwd": "${workspaceFolder}/my_folder"
}
]
}
The structure of the folders is:
my_proyect/my_folder/data/my_data.csv
my_proyect/my_folder/my_code.py
And inside the code I have the following lines:
import pandas as pd
my_data = pd.read_csv("data/my_data.csv")
I note that works perfect if I add my_folder to the path as follows:
import pandas as pd
my_data = pd.read_csv("my_folder/data/my_data.csv")
But I can't do that because when I deploy my code in AWS Lambda it works perfect. So, Any idea how to solve this issue?
VS Code takes the open folder as the workspace. I have tested according to your directory structure, and there is no problem.
I open the my_project folder with VS Code and VS Code uses it as a workspace.
Below is my 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: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}/my_folder",
}
]
}
So, please modify your cwd configuration according to your workspace.
UPDATE:
Open the folder using File --> Open Folder...
Here is your directory:
/home/my_user/my_company/my_proyect/my_folder/my_code.py
If you have my_company turned on then "cwd" is
"cwd": "${workspaceFolder}/my_proyect/my_folder/"
If you have my_user turned on then "cwd" is
"cwd": "${workspaceFolder}/my_user/my_proyect/my_folder/"
${workspaceFolder} is just a variable that represents the path to the folder you are currently opening as a workspace.
Of course you can also use absolute path directly as "cwd":
"cwd": "/home/my_user/my_company/my_proyect/my_folder/my_code.py"

Visual studio code will not activate conda env specified in launch.json

My launch.json config looks as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"python": "/Users/username/miniconda3/envs/env_name/bin/python",
"program": "${file}",
"console": "integratedTerminal",
}
]
}
However, when I run the debugger, the specified env_name is not run. Instead the default interpreter python version (3.10) is run. Why is the specified environment not being activated?

python don't save files in visual studio code

I started to use visual studio code yesterday and i'm having this problem...
Let's suppose I have this code:
myFile = open("file.txt", "w+")
myFile.write("something \n")
myFile.close()
When I run it on VScode it don't save the file, but when I run it in SublimeText it does.
do you know how to solve it?. I didn't find anything (or I don't know how to search), my launch.json look like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
}
]
}
**After I wrote that I noticed that it saves the file in "C: \ Users \ User \ Documents \ vsprofiles", but I have my .py file on the desktop, I want it to save the file in the same path where the .py file is, like SublimeText does.
For completeness here is my config file
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}",
"env": {},
"pythonArgs": [],
"stopOnEntry": false
}
]
}

vscode python launch 'current file' as module

I am trying to find a way to give current file as argument for module. I couldn't find it anywhere so I'm trying stackoverflow
{
"name": "Python: Synapse",
"type": "python",
"request": "launch",
"module": "projFolder.targetModule",
"console": "integratedTerminal"
}
Manually specifying the module argument such as above example, works. But thinking of doing that for every each file wanted me to automate it. I've tried ${file}, but it gives the file path not the module. So it didn't work. How can I launch current current file as module?
ps: answer from https://stackoverflow.com/a/57416114/6088796, but I modify a litter
add something like this to your launch.json, but if you want to run multi-module(in a different place, the only way you can do is using multi configuration, or use a plugin(see below)
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "yourmodule.${fileBasenameNoExtension}",
"stopOnEntry": true
},
You can use the extension Command Variable to get this relative directory with dot separator.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module CmdVar",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "${command:extension.commandvariable.file.relativeDirDots}.${fileBasenameNoExtension}",
}
]
}

How to start python in "-X dev" mode when debugging / running from terminal?

python3 supports -X dev startup mode.
This feature is becoming very useful for code "environment awareness" and for execution functionality.
For ex. the debug prints of asyncio module https://docs.python.org/3/library/asyncio-dev.html#debug-mode-of-asyncio
How can I configure vscode to start python with this flag ?
vscode now supports pythonArgs in launch.json, but note that it expects a format of ['-X', 'dev']:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"pythonArgs": ["-X", "dev"],
},
]
}

Categories

Resources