How to Specify a Relative Path in VScode and Run a Program

Asked 2 years ago, Updated 2 years ago, 80 views

I write python programs in Visual Studio Code (vscode) and Atom text editors.
The same thing is happening in both text editors, so I will write about vscode.

When you try to run a program that requires another file path in vscode, if you specify the path as the relative path, it will not move, but if you specify the path from the root folder (the folder at the top of the tree), it will move.

It normally appears in Explorer and works the other way around when double-clicking and running, but it does not work when you specify the relative path.

example) The root folder in vscode is "RootFolder"

RootFolder --- 01 --- test.py
      --- 02 --- text.txt


when specifying the path of "text.txt" in the "test.py" program When running in vscode, it only moves when you say "02/text.txt",
For normal execution, it only works when you say "../02/text.txt".

If you specify the root folder as "01" in vscode, the root path and relative path will be the same, so it works, but it is inconvenient that you can only handle programs in the same folder.
However, it is troublesome to open python files every time to verify operation.

By the way, if you specify an absolute path, it works either way.But this is also inconvenient...

So, is there any way to run the program using the relative path from within the vscode?

python vscode

2022-09-30 14:36

1 Answers

"Regarding the resolution in the article I mentioned in the comment, the title and the end of the question are ""how to run the program using relative paths from within vscode,"" but the explanation in the middle is about ""text.txt"" path specification, so it doesn't seem to have anything to do with ""run."""In addition, it is difficult for others to understand what the word "Run a program using a relative path from within vscode" means.

Therefore, replacing "How to set the current folder when running Python scripts in VSCode to the folder where the target script resides will help you with the following article:

Debug Python with VS Code
Make the current running directory the directory with files in Visual Studio Code

All actions are to add "cwd":"${fileDirname}" to the "configurations":{} file in the launch.json file.

However, in the above case, the condition that the folder with the script is the current folder is not very good in terms of versatility.
It is also obvious that this question is actually followed by the module import instead of "text.txt".

In order for the script to work in any folder, whether it runs from VSCode or directly from Python, it is recommended that you obtain the script file's own path in the program and create a relative path to the file to be used or add a search path to the module.
You can move the current folder by yourself.

Get the location (path) of the file running on Python___
[python] Open the file in a relative path from the execution script

The special global variable __file__ contains the script's own path, so let's take action based on it.
runpy --- locating and executing Python modules

However, please note that the Python's __file__ will fail when running in interactive mode.
There are other things like this.
Find out why __file__ does not get directory names for running scripts


2022-09-30 14:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.