skip to Main Content

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


  1. 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’s E: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 open hello_person.py, it looks for it in the working directory, since it is a relative path (specifying no directory at all). Running python .hello_person.py would be the exact same.

    So, you can either do this:

    cd "Dave Gray tutorials"
    python hello_person.py
    

    Or:

    cd "E:Python LearningDave Gray tutorials"
    python hello_person.py
    

    (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 to E: by running E: as a command – or by adding /d to the cd command as pointed out by user @mklement0 in the comments)

    Or:

    python "Dave Gray tutorialshello_person.py"
    

    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 be E: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:

    import os
    
    script_dir = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_dir)  # just like running `cd` before the script runs
    
    Login or Signup to reply.
  2. Your terminal directory is E:Python Learning>, but your script file is in E:Python LearningDave Gray tutorials. of course you can’t find the python script file in the E: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.

    enter image description here

    You need to use the official extension Python to execute the scripts. It is also recommended that you remove spaces from the directory.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search