I’m a total beginner and have just downloaded Python and VSCode. When I checked via the terminal (Mac) it recognised python3.
If you look at the error, you will see why.
It says that ‘can’t open the file [a path here] No such file or directory.’ So, it’s telling you that in the provided path there is no app.py file. And here is the explanation: on Desktop you have a folder named ‘Code w Mosh’ and inside it you have the ‘app.py’. However if you look at the path you will notice that the terminal is looking for the ‘app.py’ directly in the Desktop folder. Of course, there is no such file.
What to do?? navigate using the terminal to where the file is.
A quick terminal course:
- if you want to see the path where you are: ‘pwd’ without the ‘’. It means Print Working Directory.
- To move to a directory: ‘cd folder_name’ It means Change Directory to folder_name.
- To move back a directory: ‘
cd ..’ that is cd an space and 2 dots.
So, to solve your issue, move to the directory where ‘app.py’ is, you have to change the directory. So, type in the terminal (without the ‘’): cd Code w Mosh and then hit enter.
To confirm you are in the correct path, ‘pwd’
Finally exectute your program: ‘python3 app.py’
Let me know if you solve your issue.
Hey Nicholas, thanks for the help. When I type in: cd Code w Mosh it says no such file or directory. Apologies if this is really noob level.
The script does work when I hit the play button in the top right part of the window “run python file in directory”, but still not while running through the terminal.
The very beginning is not easy. So, no worries…
To run your Python program you have 2 options:
- Run the program using the Terminal in VS Code
- Open a Terminal and run your code there.
They are basically the same thing, but sometimes VSCode terminal is not very friendly.
Soooo, try option 2. It’s not difficult at all. To do this In Windows, using the Windows explorer, navigate to where the python file is and then at the top (where you see the path) make a click and the whole path will become blue. There you write: ‘CMD’ without the ‘’. The command terminal will show up in the correct path. So, you just write: python file.py and the file will open using python. To avoid writing ‘python file.py’ everytime you need to run your code, just press the up arrow, and the historic of commands will be there. Or start typing and after a couple of letter hit TAB and it will autocomplete. I hope this makes sense.
In this video, at 00:47, it’s shown: Windows 10 How to Open Command Prompt in Current Folder or Directory - YouTube
If running on Mac, as your image shows, it’s a bit more complicated. But follow this video and it’s a breeze. How to right click and Open Command Line Terminal at folder on Mac from Finder - YouTube
Once you open the terminal in the correct path, you just write: python3 file.py and the file will open using python. To avoid writing ‘python3 file.py’ everytime you need to run your code, just press the up arrow, and the historic of commands will be there. Or start typing and after a couple of letter hit TAB and it will autocomplete. I hope this makes sense. Observe that for Mac you have to specify Python3, since Python2 is also installed, so just type: python3 app.py
And if you feel weird handling the terminal, that’s perfectly fine: learning a new skill (like programming) will take you out of your confort zone many times. Just follow the instructions, read forums, and you will be doing this in no time!
Let me know if option 2 works better for you.
That worked. Thank you so much!!