Python - Loading a source script - python

I am brand new to python and I'm using IDLE. However, as I learn, it is very tedious to retype an entire class over and over again at the prompt while I work out small syntax errors.
I would love to simply write a .py script in notepad++ and load it from the IDLE prompt. How is this done?
I'm using Windows not UNIX/Linux or Mac

In IDLE, Crtl+n will create a new python file that you can edit your code in. When you click F5 it will prompt you to save the file, which you can save anywhere and then will execute the file.
Each time you edit, click Ctrl+S to save and F5 to run the newly updated file.

Related

Beginner question about finding Python script in IDLE shell

I'm a beginner in Python with no prior programming experience, so bear with me here.
I installed Python, started playing with it (typing variables, playing with math operations) in the Shell window and everything is fine. I open a New Window and started writing a simple script. Something like this:
print (1+1)
I press Run Module, and I am asked to name and save the script first. I do so by calling it firstscript.py, and save it to my desktop.
Now I press Run Module, and in the shell window 2 is printed on the screen. Everything is fine. I close Python, and open it up again. Now in the shell window, I type firstscript.py and I get the red message NameError: name 'firstscript' is not defined.
I can run my program only if I open the script file on my desktop and press Run Module from there, but if I try to start it up directly in IDLE Shell by typing its name, I get the error message.
What did I do wrong? Thank you.
Good to see that you are starting with python.
Firstly, you can run the file directly using 'Run Module' only when you have the file open. Once you save the file and quit, you are out of the file editor and back to the terminal.
Simply typing in firstscripty.py will not work as it does not recognize the command.
To run the file from the terminal, use the below code:
python [locationOfFile\]firstscript.py
You can check out this detailed explanation: https://realpython.com/python-idle/#how-to-work-with-python-files
The problem here is the Shell doesnt know that your firstscript.py is sitting on the desktop
The simplest way i suggest using cmd with:
python C:\Users\{your user}\Desktop\firstscript.py

How to automatically clear terminal in Visual Studio Code when running the file on terminal?

is there a way to configure VS Code to automatically clear the terminal right before I run a Python file, for example? I searched a lot for that but without success. Whenever I try to run a file in terminal, previous runs are still there in the terminal, and it gets kinda confusing. Notice that I want to clear the terminal when I run the code in terminal (i.e. when I click the play button), not when I run with debugging. My idea is to always have the terminal empty when I run a file. Any thoughts?
Currently in VS Code, it does not automatically clear the terminal data settings, but you could use the command "clear" in the VS Code terminal to clear the previous output data, or click the'kill' icon to delete the current terminal and use the shortcut key Ctrl+Shift+` to open a new VS Code terminal.

How to open a python file in Cmder Terminal Quicker?

I want to open a python file in cmder terminal quickly. Currently, the fastest way i know how is to navigate to the directory of the python file in cmder terminal and then run it by calling "python file.py". This is slow and cumbersome. Is there a way for me to have a file or exe, that, when i run it (or drag the program onto it), automatically makes the program run in cmder straight away.
Windows 10
Clarification: I'm using cmder terminal specifically because it supports text coloring. Windows terminal and powershell do not support this.
On windows you can go to the directory with the file in the explorer and then simply hold shift as you right click at the same time. This will open the menu and there you will have the option to use the command shell/powershell and then you don't have to navigate to the directory inside the shell anymore and can just execute the python file.
I hope that helps.
Answer: The escape codes just weren't properly configured for the windows terminals. You can get around this by using colorama's colorama.init(). It should work after that.

Python shell is open but not .py file?

I am using python 3.4.2 IDLE on windows.
When I open the IDLE shell and then open .py file, then it works,
but when I try to open the .py file by double cliking, it just doesn't open, or proceed anything. Looks like as if nothing has happened.
I would like to open .py file and then just press F5 to see what is going on rather than individually open all the file (I am still beginner to python, I know I can use pycharm, but at this point, just that would be good enough)
When you double click on a .py file, it will run it using the Python Interpreter. You can right-click on the file instead and choose to open it in IDLE.

Having trouble running python in cmd.

I am a complete noob when it comes to programming. I just downloaded python and I will be using Notepad ++. I have saved a file to my desktop and the file name is test and changed the extension from .txt to .py
So when I go to Notepad ++ and create a program and save it, I go to the cmd prompt making sure I am in my desktop directory and type the following
python test.py
and it tells me that python is not recognized. Any help to fix this problem would be greatly appreciated.
First thing python is indent oriented programing language and it comes with its default editor called IDLE. So if you use notepad++ instead of IDLE it might gives you a syntax error. Second thing for executing python file from command prompt,you need to setup environment variable.Please see below link for setting up environment variable.
https://docs.python.org/2/using/windows.html
You can directly run your program in IDLE without using command prompt. So i would suggest you to use pythons built in editor (IDLE)

Categories

Resources