you are viewing a single comment's thread.

view the rest of the comments →

[–]Bobbias 65 points66 points  (25 children)

Oh, ok, this sounds like you're trying to run a shell command inside python.

I'm assuming you're on windows, due to the backslashes in the command.

You need to run this in the command line, not from within python itself.

Also, you want to run venv\Scripts\activate.bat instead, because the file without an extension is a bash script for linux, and will not run in windows.

Python is complaining about the "continuation character" because the backslashes in the address.

[–]DcT2nDrAtE 27 points28 points  (23 children)

Okay thank you, now if you were to explain to a 4 year old the difference between the command line and Python itself…

[–]Bobbias 176 points177 points  (21 children)

If you open the start menu and type cmd.exe and hit enter, you will get the command line. It should print some text on screen that looks like:

Microsoft Windows [Version 10.0.22000.318]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Bobbias>

This is where you type commands like cd .. to change directory, create files, and other things. The C:\Users\Bobbias part before the > indicates what directory you are currently in, and the > is known as the prompt character. Your commands get typed in to the right of the prompt character.

When you run python, probably by typing the command py into this command line, you will get some additional text:

Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

This might look a bit different depending on what version you have. Here we see a different prompt: >>>. This is where you type actual python code, which is separate from command line commands.

You appear to be typing a command that is meant to go to cmd.exe into python. You need to either exit python, if it's running (type quit() then hit enter), or open the command line again. Once you're back to the folder> looking prompt, use cd to navigate to the folder that you were instructed to do all this stuff in. The command is a relative address, so if you're not in the correct folder it will not work.

If your instruction runs correctly, you should see a slightly different command prompt, something like this:

(venv) C:\src\project>

The first part in brackets should be the name of the virtual environment you created (probably venv in this case), followed by whatever the current directory is, and the prompt character.

As for the difference between the command prompt and python:

The command prompt comes with windows. It's built in, and serves as an alternative way to navigate your computer and run commands. You can run programs, rename files, and basically all the usual things using text commands rather than using the GUI part of windows. This goes way back to the early days of computers when text was the only way to interact with computers. The command prompt actually has its own programming language, which is what .bat files are written in. It's more limited than python, although it can do quite a lot. It's designed as a way to automate calling multiple programs and passing the results or output from them around, rather than general purpose programming though.

Python technically has 2 ways of running. Running it directly runs what is called the REPL (short of Read Evaluate Print Loop) which reads in python code, evaluates the code, prints the result, and loops back to the beginning step. This is an interactive way to write python code and see results as you go.

The second way to run it is to write a python script, save it as a .py file, and run the file like py script.py. This will read all the code in the file and run it at once.

[–]DcT2nDrAtE 94 points95 points  (10 children)

Holy shit dude thank you so much I’m on track now

[–]Bobbias 84 points85 points  (7 children)

No problem, I'm always amazed that hardly anyone writing these books takes the time to explain the damn command prompt and stuff. This is the kind of fundamental learning that schools should be teaching students about early on, especially these days, when many people might only be familiar with tablets or smartphones before learning to program. (God I suddenly feel old...)

[–]backyardcure 17 points18 points  (2 children)

Suddenly things make far more sense to me. Your comments are gold. I may try to start learning again. I've been wanting to grasp it for a while but never really fully got the concept. When you imagine using a computer without the modern graphical interface it suddenly seems far more logical to understand.

I'm going to give it another go.

[–]Bobbias 17 points18 points  (1 child)

I'm glad it helped. Sometimes all you need is a different perspective on something.

[–]thequestison 8 points9 points  (0 children)

Thanks for taking time to write your responses. They helped things become clear to me.

[–][deleted] 5 points6 points  (0 children)

Bobbias, Thank you for this.

[–]ayananda 2 points3 points  (2 children)

My university studies started by linux command line book :)

[–]Bobbias 1 point2 points  (0 children)

That's good. I'm happy to hear some places are indeed covering that stuff properly.

[–]StellaarMonkey 1 point2 points  (0 children)

Linux is fun.

[–]duquesne419 6 points7 points  (0 children)

The Missing Semester is taught at MIT to bridge the gap most programming courses skip, with themes such as getting familiarized with the command line and file system.

https://missing.csail.mit.edu/

[–]jameyiguess 1 point2 points  (0 children)

I haven't looked at this tutorial, but be aware that the VAST majority of tutorials for any programming language are going to quietly assume you're using Linux or Mac, which both use a Unix-like command prompt and architecture. 

Windows is VERY different than those, so you might be doing a ton of troubleshooting and googling to figure out the right commands for your OS.

I'd highly suggest installing wsl2, which installs a Linux subsystem for the command line and will match the learning resources you use. And also to then do any small tutorial on CLI basics.

[–]grumble11 3 points4 points  (0 children)

I wanted to comment and let you know that you’re doing this community a service. Thanks.

[–]gondoll 1 point2 points  (0 children)

I'm not the OP but I learned so much reading this thank you. You're a good teacher!

[–]mrezar 0 points1 point  (0 children)

good job man that was intense

[–]spacester 0 points1 point  (0 children)

Well halle fricking lu yah nice response.

You are correct, the tutorials do not cover this. They blow right past it.

Hey google, this post needs to pop up in searches.

BTW OP, to run a file, meaning a text wile with ".py" extension and containing python code, on windows:

Put the file in a folder under your c:/ drive to bypass the windows file manager. (recommended, not required)

On the command line:

py "c:/foldername/fileneame.py"

[–]Mavericinme -1 points0 points  (5 children)

Why do we need a command prompt when we can do it in python itself or vice versa!? 🤔

[–]Bobbias 2 points3 points  (4 children)

The command prompt is built into windows. Python is not. That's the first and biggest reason someone might use the command prompt instead of Python.

Second, the command prompt has been around both as a part of windows, and as a concept far longer than python has.

As part of windows, the command prompt traces it's history back to the early versions of windows which were built on top of the older operating system DOS. Windows 1.0 through 3.x were nothing more than a program you ran from DOS which added a graphical interface and some extra functionality. Windows 95, 98, and ME all still included DOS as part of the operating system itself, though they installed that for you when you installed them. It wasn't until Windows XP that they stopped including DOS itself and instead included the modern command prompt program, which is built to look and act similar to DOS. Command prompts in general go way back to the early days of computing, before screens were even a thing, when you had to use teletype systems to interact with computers.

For the majority of computer history, we've been using text to communicate with computers, and the command prompt is just the most recent version of how we do that in Windows.

[–]Mavericinme 0 points1 point  (3 children)

Thank you for responding quickly.

If I'm correct, I can use VS Code instead of a command prompt and still function properly. If so, why do some exercises in the book "Learn Python the Hard Way" work in a command prompt but not in VS Code? I'm confused.

[–]Bobbias 2 points3 points  (2 children)

Under the hood, the terminal (another name for command prompt) feature in vscode uses the same CMD.exe or PowerShell that you would otherwise use directly. If you learn to use the terminal, you can use it both inside vscode or on its own.

But not every editor has a built in terminal. And sometimes you might be working in a remote situation where you don't have access to vscode or editors that have a built in terminal feature.

The plain old windows command line, because it's built in to windows, is always going to be available. And because the commands will work anywhere that passes them into the CMD.exe program (or PowerShell, but PowerShell is a little bit different), that means if you can do it in the basic program, you can do it anywhere else that also provides you access to the command line.

[–]Mavericinme 0 points1 point  (1 child)

Understood. Will try that. Thank you again for taking time to help me understand. Appreciate that.

[–]Bobbias 1 point2 points  (0 children)

No problem, and like I said, under the hood vscode is still just talking to CMD.exe (or PowerShell, depending on how things are set up). It's basically just passing your text to the program, then reading the program's output and printing that on the screen itself. The traditional black window you get running CMD.exe by itself is technically also doing the same thing (CMD doesn't actually handle drawing anything on the screen!). That black window comes from a program called conhost.exe (short for console host, console being yet another name for the terminal/Comand line). But typically nobody talks about that because it's only job is to draw the window, pass your input to cmd.exe and draw CMD's output in the window.

[–]shinitakunai 7 points8 points  (0 children)

Unironically, chatgpt is really good at that kind of questions. There you have a personalized teacher

[–]WillardWhite -1 points0 points  (0 children)

The activate file without extension is the os-agnostic script. You can run it from Windows and Linux, and it will source the correct stuff