All my Python files are located in the same directory, "E:Python LearningDave Gray tutorials"
, and simply running the files works.
However, I get an error when running one within the VSCode terminal:
PS E:Python Learning> python hello_person.py
C:UsersvictoAppDataLocalProgramsPythonPython312python.exe:
can't open file 'E:\Python Learning\hello_person.py':[Errno 2] N
o such file or directory
PS E:Python Learning>
How do I go about changing the path to match this one?
I would appreciate it if you could help me figure out what’s the best way to create the proper path for future folders.
Right click on folder + Open in integrated terminal works as it gives me the correct path, but it’s not desirable given that every other .py in the folder runs just fine within the default terminal.
Here’s the code for the file as well:
import argparse
parser = argparse.ArgumentParser(
description="Provides a personal greeting."
)
parser.add_argument(
"-n", "--name", metavar="name",
required=True, help="The name of the person to greet."
)
args = parser.parse_args()
msg = f"Hello {args.name}!"
print(msg)
I tried changing the absolute path.
The program only ran when I moved the file from its folder to the one mentioned in the terminal error message.
2
Answers
The
python.exe
program starts within its own copy of the environment, like any application you start. That environment has several attributes, including one called the ‘working directory’. By default, the working directory for a program started from the command line (like you are starting Python), is the current directory for the command line interface – in your example, that’sE:Python Learning
, as is evident from the prompt.Any relative path access from within the program will be relative to this working directory. So, when
python
tries to openhello_person.py
, it looks for it in the working directory, since it is a relative path (specifying no directory at all). Runningpython .hello_person.py
would be the exact same.So, you can either do this:
Or:
(Note: this assumes the current drive is
E:
– Windows in particular has the attribute of the ‘current drive’ for an environment as well as a working directory, and is different from something like Linux or MacOS in this respect; you can of course change the current directory toE:
by runningE:
as a command – or by adding/d
to thecd
command as pointed out by user @mklement0 in the comments)Or:
And variations thereof. All of these will start the script, but keep in mind that inside the script, the working directory will be whatever the working directory for Python itself is. So, in the first two examples, it will be
E:Python LearningDave Gray tutorials
and in the last example, it will beE:Python Learning
.Finally, if you’re asking how the working directory for a script can be changed to the directory of the script itself, that goes something like:
Your terminal directory is
E:Python Learning>
, but your script file is inE:Python LearningDave Gray tutorials
. of course you can’t find the python script file in theE:Python Learning>
directory.You should store your script files in the same folder and open this folder as a workspace so that your terminal will also be in this folder directory and you won’t get this error.
You need to use the official extension Python to execute the scripts. It is also recommended that you remove spaces from the directory.