Running First Python Scripts

Hi All,

I just installed Python in my computer, and trying to do what’s suggested in the Codecademy page, as follows:

1. Open a Terminal (if on Mac) or Command Prompt (if on Windows) and browse to a directory of your choice.
2. Create a new file called mycode.py and open it with your favorite text editor.
3. Copy and paste the following code and save the file.

print(“I’m running Python code on my own environment!”)

4. Finally, run the script by executing the following command from the Terminal/Command Prompt in the folder where the file was saved.

python mycode.py

5. Our console should print the following message

I’m running Python code on my own environment!.

6. That’s it! We just ran Python code on our own personal environment.

Now how do I create the mycode.py from the windows command prompt?

My questions are:

  1. Same as above, how do I create the mycode.py from Windows Command Prompt? I have already at the step where I saved the script in text editor (Notepad) and saved it in the same file as the mycode.py folder. In the instructions, it says :
    “Finally, run the script by executing the following command from the Terminal/Command Prompt in the folder where the file was saved.”
    How do we run Command Prompt in the folder where the file was saved? Right clicking in the folder doesn’t allow for option for Command Prompt…I tried choosing “Git Bash Here” but it doesn’t seem to be the way of doing it
  2. Do we always need to do the scripts in Windows Command prompt (for WIndows) to run Python?
  3. When the instruction says : " 5. Our console should print the following message", does the “console” means in the Python program?

Thank you all for your help. I tried following the answer but I really cant figure it out…

  • Vinaa

It might be worth considering a tool other than the basic command prompt cmd.exe for this. Windows powershell might be a decent option or gitbash if you prefer bash (but they aren’t the only options).

  1. You’ve already created the file? That’s good. You can create files directly with the basic windows command prompt with options like echo text > myfile.py but for anything more complex you’d probably want to be using some sort of text editor, say Noteped for a very simple GUI option but there’s a lot of ways to do the same (you can start text editors directly from cmd if you like).

    Part II: the easiest option is to open a terminal and simply change your working directory to the folder where you saved this file. There is a command cd for (c)hanging (d)irectory, if you’ve never used it before you may need a web search for some details.

  2. No, you don’t always have to use a a terminal but it tends to be the easier option since any errors by default get pushed to the same terminal which makes debugging quite fast. If you look into a more complex IDE then you could have a self-contained program that emulates terminals as and when you need them (vscode, pycharm etc.) for the best of both worlds. Navigating your system through the command line is a good skill to learn though.

  3. Likely it refers to the same terminal window you ran the program in so you’d have your own line python myprogram.py that runs and uses the same window for output.

Here is a rough idea of how instruction 5 should output, first line is the command you wrote, second is the output from running the file:

C:\path\to\username: python myprogram.py
I’m running Python code on my own environment!.
C:\path\to\username: 
1 Like

Let’s say you installed IDLE in your User account in Windows. It will be installed in the AppData folder of that user. We can navigate to it,

C:\Users\Username>cd appdata\local\programs\python\python38\scripts

Your install is likely Python 3.9 so change the 38 to 39. The above command will navigate to the Scripts folder within the python install folder. This is the best place to keep your scripts while you get comfortable with Python.

So now the command prompt will look like,

C:\Users\Username\appdata\local\programs\python\python38\scripts>

To create an empty file, use the command, (be sure there is whitespace around the redirect operator, >)…

type nul > mycode.py

and Enter. Now check the directory to see if the file is there…

dir

You should see your file in the list. Now you can navigate your text editor to the folder and open the file in text editor.

There is another way to write the file without using the text editor since it is only one line…

echo print(“I’m running Python code on my own environment!”) > mycode.py

Now we can run the file…

C:\users\...\scripts>python mycode.py
I’m running Python code on my own environment!

Bottom line, save all your files in the Scripts directory (we can create new folders in there, as well) so they are all in one place and easy to find. Your text editor should be keeping track of the most recent files so you won’t have to dig around.

You can also navigate to the Scripts folder using Windows explorer and then open the CMD window from there. I’ll leave you to explore this on your own.

The console is the terminal. Python only ran long enough to produce the output then terminated. To open Python’s interactive shell in the terminal, type and enter,

python

To exit the shell, use CTRL + z and enter. That will restore the command prompt.

Hi,
I decided to re-do and followed your suggestion:

  1. Instead of using Windows CMD, I used GitBash. Then I did:
    echo "I’m running python code on my own environment > mycode.py
  2. Then I type:
    python mycode.py
    but nothing happens…I am confused…

Hi,

I have trouble understanding this…
My IDLE is already 3.9 version, but I don’t get how to get to the “scripts”, I typed below:

C:\Users\vinaa\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.9\scripts
but returns an error.

and then I just went ahead to run the code

type nul > mycode.py

it also returns

nul > mycode.py
Traceback (most recent call last):
File “<pyshell#0>”, line 1, in
nul > mycode.py
NameError: name ‘nul’ is not defined

so I am now even more confused as to what the meaning of this…

You’ve got some quotes going walkabouts in that image there, make sure you close the ones you use. Quoting rules and escapes inside the shell are quite complex. It might be best to split these tasks up. For Python, maybe just write a simple text file in Notepad or your choice of text editor with (make sure it is saved as plaintext and not rich text, word documents or otherwise).

Bear in mind that the text content of the file should be valid Python. If you want to print output then you’d probably want to use the print function inside the file, if you want to print a string make sure the creation of the string is valid Python.

If you’re unfamiliar with the command line it might be worth looking into a course on getting to grips with it. I think you might be combining too many new things at once here.

what do you mean by “Valid Python”?

That path looks a little off. Have you got a shortcut to IDLE on your task bar, or just the Start Menu? If just the start menu, pin it to the taskbar for easier access. Open the interactive shell, and click File, the Open.

It should take you to the install folder, in which you should see a Scripts folder. Click to open that folder.

Now look at the location bar of the explorer window. Copy that path and paste it in to your text editor. How does it compare to the path you have above?

Also, the commands that work in CMD likely won’t work in bash, and they definitely won’t work in Python. The traceback above is Python.

Note that in CMD, echo writes a line to the console unless redirected to a file. The line we write to the .py file needs to be Python.

echo print(“I’m running Python code on my own environment!”) > mycode.py
#    -------------------------------------------------------
#    This is a valid Python statement