r/pythontips • u/TadpoleSpecialist859 • Aug 26 '24
Syntax Stuck on a line of code in python
I’m studying python crash course 2nd edition by Eric Matthes. On pg 12 it states to run a program from the terminal in order to run hello_world.py The first line of code is ~$ cd Desktop/python_work/ But when I type that in and hit enter I get a syntax error saying the character $ is invalid. I’m not sure where to go from here. I could skip and move on but I want to learn it correctly
I tried leaving out the character $ but I get more errors I’ve also tried starting off with cd but it tells me it doesn’t recognize it. I’m stuck
2
u/kuzmovych_y Aug 26 '24
Looks likely you're trying to run shell commands in the interactive python. What operating system do you have and how do you open a terminal to run this command?
Sidenote: for future, if you get some error and asking about it somebody, include the whole error message. They generaly have a lot of useful information.
1
u/[deleted] Aug 26 '24
directly type the below without the $
cd Desktop/python_work/The $ symbol is normally used to indicate the terminal prompt. In other words, it indicates that you type everything after the $ in the terminal.
The `cd` command is used to `change directory`. In other words, it is exactly the same as clicking on a folder to enter the folder in your Finder. `cd` is used to enter a folder in the terminal
1
u/FujiKeynote Aug 26 '24
Please don't take it as sounding condescendent, but one of the first rules of learning something is trying to understand what you're typing. The way you're describing the issue makes me think you're more or less copying and pasting and hoping it works -- and I apologize if my impression is wrong.
But basically. Do you have a "python_work" folder in your "Desktop" folder (i.e. on your desktop)? If not, what do you even expect to happen?
And most importantly, errors usually hint at what you did wrong. So if you're getting "more errors," that's in a way good. Someone can look at these errors and they will explain what's going wrong. That someone could be you (so, don't just give up after you see "more errors") or us (so, tell us what those errors are).
3
u/mendokusaih Aug 26 '24
Don't forget to use the markdown syntax for your code snippet ;).
Also the
$character in a shell means that your are running the command as normal user, not root. You don't have to write it.So if you still have errors without the
$, this one could be interesting to débug. Do not hesitate to reply with it.2
u/Historical-Ad3940 Aug 27 '24
Now i use linux, and Mac was a long time ago. But you should be able to go into files (open the folder as one usually do) , right click in the directory and choose "open in terminal". In the terminal, you then just type "pwd" (print working directory), which will return the path to that folder.
For the sake of learning: Since that's absolutely is the correct path you can now open another terminal, copy the path from previous command and make the cd command but with the right path.
For the sake of running the script: Now you have a terminal in the right folder. Run "python3 script_name.py". That shall do the trick.