How to make VSCode always run main.py - python

I am writing my first library in Python, When developing I want my run code button in VS Code to always start running the code from the main.py file in the root directory. I have added a new configuration to launch.json however I seem to be unable to use this configuration as default. How can I do this/

You need to put the 'launch.json' under the '.vscode' folder inside your workspace. Then Run > Run Without Debugging (shortcut on windows CTRL+F5)
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": "${workspaceFolder}/main.py",
"console": "integratedTerminal"
}
]
}

You can modify the launch.json with the below settings for key program. You may want to point program to the file which you want to execute. In the below case main.py is present in my workspace folder only. You can modify it as per your requirement.
{
// 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": "${workspaceFolder}/main.py",
"console": "integratedTerminal"
}
]
}

I found the right solution is to just change "program" in launch.json to:
"program": "main.py",
If trying to add the {workspaceFolder} it gives a FileNotFoundError.

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"

How setup lauch.json for VS Code so I can use other folders/files in the current working directory?

I'm trying to write python code that needs to read files in the same current working directory. I'm new using VS code so I don't really know how to set it up have the launch.json the following way, but it still doesn't read other files in the same cwd. HELP :'v
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",
"cwd": "${workspaceFolder}"
}
]
}
${cwd} - the task runner's current working directory on startup
The default setting of 'cwd' is the "${workspaceFolder}". In VSCode, the relative path depends on the setting parameter 'cwd', unless you use an absolute path. It doesn't care the relative path to your python file, it just cares the relative path to 'cwd'.

I got a wrong directory using os.path.abspath('.') [duplicate]

How do I run a Python program under debug and set the working directory for the run?
#SpeedCoder5's comment deserves to be an answer.
In launch.json, specify a dynamic working directory (i.e. the directory where the currently-open Python file is located) using:
"cwd": "${fileDirname}"
This takes advantage of the "variables reference" feature in VS Code, and the predefined variable fileDirname.
If you're using the Python: Current File (Integrated Terminal) option when you run Python, your launch.json file might look like mine, below (more info on launch.json files here).
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
},
//... other settings, but I modified the "Current File" setting above ...
}
Remember the launch.json file controls the run/debug settings of your Visual Studio code project; my launch.json file was auto-generated by VS Code, in the directory of my current "Open Project". I just edited the file manually to add "cwd": "${fileDirname}" as shown above.
Remember the launch.json file may be specific to your project, or specific to your directory, so confirm you're editing the correct launch.json (see comment)
If you don't have a launch.json file, try this:
To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.
Per #kbro's comment, you might be prompted to create a launch.json file by clicking the Debug button itself:
When I clicked on the Debug button on my navigation panel it said "To customise Run and Debug create a launch.json file." Clicking on "create..." opened a dialog asking what language I was debugging. In my case I selected Python
Configure the cwd setting in launch.json as follows:
{
"name": "Python",
"type": "python",
"pythonPath": "python",
...
"cwd": "<Path to the directory>"
...
}
This setting helps me: (I am a Windows person)
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceFolder}\\app\\js", // set directory here
"program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}
In some cases, it might be also useful to set the PYTHONPATH along with the workspaceFolder:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
I am posting this sample configuration for people who use TypeScript on Node.js
in my project my Node.js server TypeScript files are located in folder Application_ts
and the compiled js files are generated in the folder named Application
because when we run our application in debug mode or start it normally we should start from Application folder which contains the js files
so bellow configuration run debug from root folder where my application_ts also exists and works perfect
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug TypeScript in Node.js",
"program": "${workspaceRoot}\\Application\\app.js",
"cwd": "${workspaceRoot}\\Application",
"protocol": "inspector",
"outFiles": [],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": [],
"sourceMaps": true
}
]
}
You can set up current working directory for debugged program using cwd argument in launch.json
To set current working directory to whatever file you are executing at the time:
File > Preferences > Settings > Python > Data Science > Execute in File Dir
Thanks brch: Python in VSCode: Set working directory to python file's path everytime
I faced the same issue and noticed that when running the which python command in Terminal in Mac it shows me a different path to what I get when I run the which python command in vs code. And also that my file runs properly in the terminal when run using python filename.py
So I copied that path from the terminal and pasted it in VS code into Preferences->Settings->Extensions->Python->Default Interpreter Path and it worked. I hope this helps.
I use the "justMyCode = false" so I can also debug and jump into the functions that the main script calls.
{
// 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": false,
"cwd": "${fileDirname}" }
]
}

VS Code, how to specify the startup file for python

I am running a Python app in VS Code and every time I run it I have to open the file where the code starts.
Sometimes the file I am looking at is different and it is annoying to have to always remember to open that page again.
To fix this I tried to change the launch.json file but it does not work;
{
// 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
// "program": "${file}",
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/lecture1nn.py",
"console": "integratedTerminal"
}
]
}
If you're trying to have a consistent script to execute at startup, you can define a sitecustomize module which the site module will always import. That way you can have the code in sitecustomize do what you want at start-up as a side-effect. See the site module docs for details.

python scripts debug in ROS with Visual Studio Code

In order to debug python scripts, I would like to run "source path_to/setup.bash" before python debugger begins. How should I to do so ? Thank you!
VSCode enables you to do this by setting a preLaunchTask in your launch.json
What you need is a task.json with a type set to shell. It looks something like this.
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "prerun",
"type": "shell",
"command": "source hello.sh" // your shell command here
}
]
}
Remember the label "prerun". Now in your launch.json, alt Launch configuration, set this label as preLaunchTask like
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 Experimental: Current File (Integrated Terminal)",
"type": "pythonExperimental",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"preLaunchTask": "prerun"
},
]
}
You can tailor your debug configuration for your needs. For the example above, it pre-launches hello.sh every time Python source file is the editor is in debug. Now switch to your Python code and continue debugging. Hope this helps.

Categories

Resources