Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Running Python Code From the Command-Line

Learn how to write and execute code from the command-line. You’ll see how to create, edit, save, and execute a script on Windows Ubuntu and Mac OS.

00:00Next, you’re going to see how to run code from the command line, first in Windows.So, holding down Windows, tapping R, and then typingcmdwill get you to the command prompt.

00:16Now you can change into theDocuments/ folder withcd Documents,and you can make a new directory withmkdir and then the name of the directoryyou want. Here, it’srealpython.

00:27Change into that directory withcd, and then make another directorycalledrunning_scripts. Again, change into that directory.

00:42Now usingdir,you can see that’s an empty directory. And to create a new empty file,we need to use a little trick. We’re going to usecopy and thenNULwhich isnull, an empty file—and enter the name of the file in question.

00:57Socopy NUL hello.pycreates a zero-byte file calledhello.py.Now you can edit it using the commandidleand then the name of the file that you’ve created—here,hello.py.

01:20In a few seconds,you’ll see IDLE open up, and now you can type the commands you want. Here, again,the classic'Hello World!'Save the file with Control + S,and then you can close the window either with theX or by doing Alt + F4. Now,you can run the file by typingpython, a space, and then the name of the script.

01:43There you can see,Hello World! has run,and now you can exit by typingexit and hitting Enter.Now, you’ll see how to run scripts from the command line using Ubuntu.

01:56Right-click on the desktop and pickOpen Terminal.And now change into a directory.So here, I’m going to change into therealpython/ directory and therunning_scripts/ directory. And you can see withls that it’s an empty directory—there are no files in there.

02:15And you can use thetouch command to create a new file, sotouch, space,then the name of the script. And if you list again,you can seehello.py exists, and listing with the-al switch allows you to seethat the file has a size of0.

02:28And now you can usenano to edit the file by typingnano, space,and then the name of the file you want to edit.So here we’ll type again the classicprint('Hello World').

02:40And you see you get some color-coding of syntax in this editor,so you can write it out using Control + O and then hitting Enter to save it.And then Control + X will exitnano. Back in Terminal,you can run it with the commandpython3 and then the name of your script.

03:03As ever, typeexit to quit the terminal.Now, running code from the command line on macOS. Command + Space searches for an app,and you can type “term” to open up a terminal.

03:20And you’ll see the commands are the same as when using Ubuntu.Opening directories, listing withls.

03:34Thetouch command works in the same way as Ubuntu,creating an empty file.

03:52And you can usenano in the same way to edit the script.

04:03You’ll notice there is no color-coding by default in the Mac version ofnano.But the commands work the same way—Control + Oand then Enter to write the file, and then Control + X to exit.

04:20python3 and then the name of the script will get it to run,and then you can exit the terminal withexit,using Command + W to close the window after it’s finished.

Avatar image for haydnvose

haydnvose onOct. 10, 2020

When I go to edit the file I get an error that reads:‘idle’ is not recognized as an internal or external command,operable program or batch file. What could I have done wrong?

Avatar image for Darren Jones

Darren Jones onOct. 13, 2020

@haydnvose - can you give me some more info - what operating system you’re on, and which version of python you have installed (you can do that by typingpython3 at the command line, and you should be in the REPL which will give you a python version). You can get out of the REPL by typingexit()

Become a Member to join the conversation.

Course Contents

Overview
50%

[8]ページ先頭

©2009-2026 Movatter.jp