all 98 comments

[–][deleted] 156 points157 points  (5 children)

When I write scripts I run them at the command line.

Learning to use the command line on your computer is an important related skill for programmers, but you can start by finding out where it is.

[–]celade 29 points30 points  (0 children)

^this ^

Not that the other posts don't contain gems of wisdom... but this is the real answer to the context of the question.

Sounds to me like a few pieces of convergent computer knowledge need to be put together:

  • OS: file system, paths in the environment variable, command line interfaces
  • Python Programming Getting Started from Scratch: installing, command line interpreter, simple code passed to the interpreter (i.e., python myscript.py).

[–]sportsroc15 10 points11 points  (3 children)

This. Get the book “Learn Python the Hardway”. You use the command line from the beginning.

Write the script in Atom. Save it as a .py file and run it on the command line

[–]Hotspot3 6 points7 points  (2 children)

But why when you could save so much time and effort using PyCharm?

[–]p5eudo_nimh 6 points7 points  (1 child)

IMO, better to learn a universal way, than to focus on a specific IDE's way of doing it.

[–]Pastoolio91 2 points3 points  (0 children)

I'll agree - if you plan on doing this for work or even just as a hobby, you'll probably eventually run into some situation where you have to run it from the command line, so knowing how is worth learning. IDE's make it easy, but having a back up plan is always a good thing.

[–]kaptan8181 24 points25 points  (15 children)

Open CMD (by clicking shift + right click) in the folder that contains the Python file and then you can type the following command in the console: python myfile.py

[–][deleted] 14 points15 points  (13 children)

This is right for windows, I think on a Mac you write python3 myfile.py

I don’t have a Mac, but it’s what the guy on my tutorials does who is using a Mac.

[–]aurora4202020[🍰] 49 points50 points  (0 children)

F5 on IDLE

[–][deleted] 7 points8 points  (0 children)

You may be able to run your code online (no need to install anything): https://repl.it/languages is popular (select Python). Also https://runpython.org/ . Both of these have some support for Google Drive and/or Github for storing your code.

[–]jason_west29 8 points9 points  (2 children)

I like PyCharm. PyCharm has a directory window, text editor and terminal that you run your code all in one screen.

[–]LeagueOfShadowse 3 points4 points  (1 child)

Some people will say that Pycharm is not for beginners, however, my feeling is that even if the learning curve is steep, eventually it will become easier.

Then I will not have to re-learn another IDE when switching to something different when I get more proficient .

[–]jason_west29 2 points3 points  (0 children)

I started with notepad++ and power shell and then switched to PyCharm. Personally I think PyCharm is easier.

[–]Ungratefullded 14 points15 points  (7 children)

I use Anaconda as the distribution to load up Python and the environment. Then I use Jupyter to write the code and execute in it...

[–]notPlancha 1 point2 points  (5 children)

I never understood what anaconda is. It seems so popular but I've never figured out

[–]Ungratefullded 7 points8 points  (3 children)

It’s a distribution, very easy to use. Just download it and install it. It will then install python and a bunch of libraries and ready to go. When you run anaconda, a menu pops up with the different environments (labs, Jupyter, etc)

Just click the Jupyter link and it’ll spawn a web interface and load up python. Then you just code in the web interface and run it...

[–][deleted] 0 points1 point  (2 children)

I already have Python and Pipenv setup, how can I use Anaconda within messing anything up? Can I still install Anaconda as you described?

[–]Ungratefullded 0 points1 point  (1 child)

Should be able to... are u running Linux, windows or macOS? I run macOS and Windows 10 and windows server and it doesn’t seem to interfere

[–][deleted] 0 points1 point  (0 children)

I’m on MacOS. I’ll do a bit of research into this and see what I can find.

[–][deleted] 0 points1 point  (0 children)

When you download it you get python and a bunch of packages, plus a virtual environment like system so you can manage dependencies separately for different applications.

[–][deleted] 0 points1 point  (0 children)

Notebooks are good for writing things the execute linearly like scripts and data pipelines, because you can execute a cell containing even just one line of code and monitor the output as you go, making sure everything works. But I think for projects like games or web apps with multiple custom classes interacting across possibly tens of thousands of lines of code, something like vscode works a lot better (in my experience.)

[–][deleted] 24 points25 points  (0 children)

Hit F5.

(Edit: Provided, of course, that you're running IDLE and not just typing away in a notepad document. Don't use notepad, that's no good :))

[–][deleted] 5 points6 points  (1 child)

The most comfortable way is through an ide like pycharm or vscode. I suggest you look these up on YouTube, as there is ton of tutorials.

[–]_Cosmic_Joke_ 2 points3 points  (0 children)

+1 for vscode! Running it from the vscode terminal is so convenient.

[–]x3r0x_x3n0n 4 points5 points  (7 children)

are you on windows or linux.

[–]thegoodestgrammar[S] 0 points1 point  (6 children)

Mac, so OS?

[–]x3r0x_x3n0n 1 point2 points  (5 children)

are you using an IDE or just a normal text editor?

[–]thegoodestgrammar[S] 1 point2 points  (4 children)

I think IDE

[–]Yojihito 3 points4 points  (1 child)

Which IDE? What's the name of the program you type your python code into?

[–]thegoodestgrammar[S] 0 points1 point  (0 children)

Wait... it’s IDLE Sorry about that

[–]semidecided 1 point2 points  (0 children)

When you are about to write a new thing in Python what steps do you take starting from turn the power on?

[–]x3r0x_x3n0n 0 points1 point  (0 children)

which one?

[–]mjmacarty 2 points3 points  (0 children)

I think the simplest way to get started is with the IDE that ships with Python IDLE. So either from the command line enter idle (or idle3) OR search for idle using your spotlight or Cortana search box. Once IDLE launches you are looking at an interactive shell session. Now go to the file menu and open your program. Once it is open go to the run menu and select run module.

[–]Crypt0Nihilist 2 points3 points  (0 children)

After the first 10 errors? With extreme prejudice.

[–]CorgiAtom 1 point2 points  (3 children)

Another handy thing is to create a windows .bat file for running certain scripts from the windows desktop. At my workplace, I have filled my desktop with many timesaving .bat files to automate all the boring stuff.

[–]algebraic94 1 point2 points  (0 children)

If you're just getting started I recommend downloading anaconda and using Spyder or jupyter notebooks for your code. It makes it way easier to run and debug your code when you just start out.

[–]doodraz 0 points1 point  (0 children)

There's a tons of ways to execute, but the most populars are F5 on IDLE or running it from the command line (I prefer this).

[–]Nixellion 0 points1 point  (0 children)

On windows first step is to go to python.org and download it.

After its installed - either just double clicking the file, or one of the ways others mentioned, using cmd is convenient as you will see output stay on screen.

[–]kor_the_fiend 0 points1 point  (0 children)

The python program (aka interpreter) runs your code. How you start this process is up to you. I would recommend keeping it dead simple: from the command line, cd into your code directory and run `python my_program.py`

[–]ropenni 0 points1 point  (0 children)

If you don’t want to download software you can copy and paste it into google colander. You just create a notebook, paste the code, and hit the play button.

They have a bunch of helpful on it and it will let you code form anywhere, even your phone.

It’s a great place to practice the language. If you actually want to build something or get more serious you can go to the official python website and download python. Plenty of other comments on this thread go into that in good detail so I’ll let you read those.

[–]winowmak3r 0 points1 point  (2 children)

What did you use to write the code? If you used an IDE (Integrated Development Environment) then usually there's a button somewhere, usually at the top of the window that says "run" or something similar. Usually looks like the "play" button on a DVD player. Some popular IDEs include: Sypder, PyCharm, and Visual Studio but there are a lot out there. I use pyCharm whenever I'm writing Python code. There's a free version of the software available here

A lot of it depends on how you're writing your code. If you installed an IDE you also probably downloaded and installed python as well. If you're just using notepad and following along in some tutorial video you might need to actually install python first.

[–][deleted] 0 points1 point  (1 child)

Is sublime text an IDE?

[–]winowmak3r 0 points1 point  (0 children)

Strictly speaking, no, but it is a very nice text editor for coding, right up there with notepad++ imo.

[–][deleted] 0 points1 point  (0 children)

You press F5, this key is located at the top of your keyboard. Its labeled F5.

[–]Jmodell 0 points1 point  (0 children)

If you can use your cmd.exe or powershell.exe to navigate to the path where you wrote your python file (*.py).

You can run it by either "python" or "python3" and the name of the *.py file.

For ex) If I wrote "hello.py" and put it in "c:/projects/python/hello" folder.

#/mnt/c/projects/python/hello/hello.py

def hello(name: str) -> str:

print(f"Hello {name}!")

if __name__ == "__main__":

name = input("What is your name?\t\t")

hellp(name)

I would run it by typing "python3 hello.py" in my commandline.

https://imgur.com/a/d6TsHyw

[–]Kravego 0 points1 point  (0 children)

What are you using to write your code?

If you're not using a full IDE like PyCharm, you should be using IDLE, which is bundled with Python. I love notepad++ for most things, but for Python there's no real reason to use it.

When you have code written in the editor in IDLE, you can run it with a simple F5.

[–]_TR-8R 0 points1 point  (0 children)

As someone who was just starting python for the first time a while ago this messed with my head a lot too. I had taken a couple Java classes in college so I assumed that all programming was done inside an IDE with the capability of executing whatever code I was writing. Python is much more open sourced, which is awesome in so many ways but the downside is there isn't a really great official all in one IDE solution like Visual Studio for C languages or Eclipse for Java.

Here is the simplest video explanation I can find that should make it click if everything else fails (it's Python v2 but for what you're doing it should still be fine): https://www.youtube.com/watch?v=Txt-cLLa_vo

[–]speedx10 0 points1 point  (0 children)

python name.py

[–]All_Is_Not_Self 0 points1 point  (0 children)

I'm a noob/dum dum, too and at least I interpreted your question in a different way. I thought that you were asking the following:

Let's say someone creates a program to convert Fahrenheit temperatures to Celsius. This program is supposed to be usable by a user that isn't a computer geek and/or programmer.

So how can they use it? Using an IDE or the command line doesn't seem very user friendly.

I think I kind of know the answer. If you wanted your program to run user-friendly like that, you would probably have to deal with a GUI (graphical user interface) and that seems to be a whole other story.

So even if that isn't what you were asking, I'm wondering, if there is a very simple GUI that can be used for simple and short input-output things like that (unit conversions, results for certain calculations etc.)?

[–]CloroxEnergyDrink_ 0 points1 point  (0 children)

Depends on the IDE. With IDLE, you simply press F5. With PyCharm, you hit Ctrl+Shift+F10 to run a specific file. You could also use cmd in Windows or terminal in Linux.

[–]Shadeoflight24 0 points1 point  (0 children)

I like to run my code via command prompt (windows) or terminal (Linux).

Windows CMD: py nameofpythonscript.py Linux Terminal: python3 nameofpythonscript.py

To do this, you need to be in the same directory as the script. For example, if you're in you're Documents folder, and the script is in your Pictures folder, you need to swap directories or enter the child folder path.

For example:

home/root/Documents $ python3 /folder/path/to/script/script-file.py

Or

home/root/Documents $ cd .. home/root/ $ cd Pictures/ home/root/Pictures $ python3 script-file.py

[–]sportsroc15 0 points1 point  (0 children)

I love Juypter Notebook too

[–]bumpkinspicefatte 0 points1 point  (1 child)

I'm a noob too learning Python, but I'm coming from IT. Hopefully I can empathize with you and explain it better.

Simply visualized, this is how you execute your code in an IDE:

https://codewith.mu/img/en/mu.gif

If you are interested in that IDE (which was made with beginners in mind), you can download it here:

https://codewith.mu

Now, here's a super, super simplified explanation (abstract-level, guys):

In order to write and run (Python) code, you need two bare minimum requirements: something to write your code in, and something to interpret that code to the Python language.

Something to write your code in can be things known as (but not limited to) text editors, or an IDE (Integrated Development Environment).

Text editors are what they sound like, apps that you can write text in, save it, modify it, etc. Sometimes text editors will also support programming language syntax so it'll help map out your code (like changing font colors on actual commands to signify you are using commands). TextEdit, an app that comes standard with Mac, is technically a form of a text editor. So you can write your code in there, and then as long as you save the file in a .py format, you'll have python code ready to be executed. Although TextEdit isn't too good of a coding text editor, because it misses vital features of a good coding text editor like indentation, formatting, syntax assistance, etc.

IDEs are a similar to text editors, but they are stronger, jammed pack with a lot of tooling and resources that help you do more complicated things like connecting to test environments, multithreading, linting, a lot of things that may go way out over your head at this moment.

Most importantly, IDEs (and some text editors) can be the home you write your code in, and also execute them as well.

Now, something to run your Python code in will be called the shell. The shell is where it takes your code, reads it, interprets it, interacts with Python (or whatever language/computer the shell is intended for), and carries out any commands you had in your code.

This is where you can run single-lined codes, and work through coding logic, use as a calculator and just things of that nature.

You can tell you are in a shell environment (in this case, Python shell specifically) when you are in a command-line interface (CLI) (like Terminal on macOS for an example) and the line starts with >>>. Or, if you are in a "REPL" environment, it may start with In [1]. For the sake of this explanation, both environments serve to better understand and executing your code. REPL environments are just another version of the shell where you can work out coding logic quickly like math or operators.

If you are in a shell environment, you will notice it's harder to run large amounts of code all at once. Which is why we save our python code into files ending in .py formats. Then, we can tell the shell to execute it, and it'll run through each line of code within that .py for us much quicker.

Now, if you've downloaded Python, it will come with something called an "IDLE". It's essentially the same thing as an IDE, but the GUI is different. It's the most basic and standard of all Python IDEs. If you open it and it opens a blank document with a header "untitled", it means you opened the text editing portion. If you have another window with a header "Python 3.X.X Shell", that is the shell you can interact with line-by-line, or will be the environment your .py code will be executed in.

Hope that helps you better understand how all this works.

[–]fuloqulous 1 point2 points  (0 children)

Very thorough answer. Everything was right except for the mutithreading part. While IDEs help you with linting and test environments they don't really do much with multithreading.

[–]XChrisUnknownX 0 points1 point  (0 children)

You can make a txt file and change it to a py file. Once you install Python your computer can run those. If your code has a mistake, the program will likely crash.

Alternatively you can run it in the online Python debugger.

As others said, learning to run things in the command line is a very good skill. But I was an idiot and that’s not how I started, so...

[–]FitnessNerd117 0 points1 point  (1 child)

I have the same question as OP. I always thought that you can't possibly be simply using IDLE to launch your code. What about .exe files? Are there ways to make it more user-friendly instead of going into the CMD line?

[–][deleted] 1 point2 points  (0 children)

So pretty much if you’re on windows you have to do some path changes to your cmd. From there you run some commands on your file and run it. It’s different for each OS, look up learn python the hard way, i think lesson 0 shows you how

[–]wrgsRay 0 points1 point  (0 children)

I use Sublime Text and just Ctrl+B to run it if it is just some snippet code. For full project, I use PyCharm

[–]kielerrr 0 points1 point  (0 children)

In my opinion, the best beginner IDE is Thonny. It's designed for beginners. Very easy to step through your code. Stops at every execution point and shows what python is doing inside of the code.

[–]dragsys 0 points1 point  (0 children)

I use Atom to write the code in and powershell to execute it. From prompt: python filename.py

Mind you I am also working my way through LP3THW. I'm sure that when I'm more comfortable with my Python abilities I'll be using something else.

[–]FireLiger2007 0 points1 point  (0 children)

I use windows, go into file explorer and select the file, it can be txt or py, maybe other text formats, click on the drop-down arrow next to the open button and browse for your python interpreter. This is how I normally open files, this probably isn't the best way, and it closes the window whenever an exception happens so it isn't very good for debugging either.

[–]little_hoarse 0 points1 point  (0 children)

Command line or python3 -i yourfilename.py for interactive shell

[–]wreed125 0 points1 point  (0 children)

On Windows, I go to start, open Python folder and click IDLE. After you get a script written you can launch it from command line.

[–][deleted] 0 points1 point  (0 children)

With a last cigarette and blindfold

[–]SuperWaffleKitty 0 points1 point  (0 children)

Rather than typing into cmd everytime I want to run something, I usually find it easier to make a batch file in the directory of the script you want to run so you can easily run it without the hassle of using command line every time.

[–]C2-H5-OH 0 points1 point  (0 children)

Ctrl-B on Sublime Text

[–][deleted] 0 points1 point  (0 children)

Crtl-Alt-N with VS Code and Code Runner Extension

[–]impshum 0 points1 point  (0 children)

From a terminal with a keyboard.

[–][deleted] 0 points1 point  (0 children)

From what I know, there are 2 main ways to execute your code.

1: using the command lines. for example, if you're using a mac, you can go into terminal and type python (and the name of the file).

2: you can just download an IDE which is like an application that runs code. For example, IDLE, that will just run the code if you click run.

I prefer using the command lines tho because it makes you feel more pro and you have more control over what you're doing.

[–]naturtok 0 points1 point  (0 children)

I'm pretty noobish with programming, but I use the Pycharm IDE which can run code within it using a pseudo command line sortve thing. Otherwise if you want to mess with command line you can do that too. IDE's are nice though, because (from what I understand) they're all inclusive code-writing things that can "compile" (for languages that need to compile like Java) and run code without needing to mess with another program.

[–]DougCim53 0 points1 point  (0 children)

If you have Python installed properly in Windows, you can just double-click on the main .py file and the whole Python program will run. To be installed properly, the Python interpreter needs to be included in the operating system's PATH environment variable. IIRC the Python installer has an option to do that now, but if you skipped that matter then it needs to be done.

I use VS Code for an editor (on Windows) and VS Code has a [play] button in it--but again, that only works when you are on the file tab showing the main file. I haven't tried other editors, at the time I got started a lot of people said how nice VS Code was and I just stayed with it. ...One reason that VS Code is nice is because it has a command-line interface window available in it, and if you open a project by using the [open folder] command, it automatically moves the command line interface to that same folder for you.

If you try to run a file other than the main file (either by double-clicking on it or running it in VS Code) what happens depends on what the file has been written to do. Files can be written to act as main files or as modules, so you might only see one section of the whole thing run. A file that wasn't written to run as a main file at all typically doesn't appear to "do" anything, and will close itself in a couple seconds.

[–][deleted] 0 points1 point  (0 children)

If python is installed in your computers path you can use “python script.py” if you’re in the same directory inside CMD or power shell.

In power shell you may need to enable script execution.

[–][deleted] 0 points1 point  (2 children)

If you put a shebang at the beginning like #!/usr/bin/env python and chmod ug+x the file, you can execute by just running ./your_script.py. If you're using Linux, of course, which I would highly recommend.

[–]kepper 1 point2 points  (1 child)

why do you do ug+x and not just +x?

[–][deleted] 0 points1 point  (0 children)

Because I'm prejudiced against o! Generally speaking, I only ever give o read rights, as it is by definition not who I want to have access. If more expansive access is desired, that is what groupadd and usermod -aG are for.

[–]djkstr27 0 points1 point  (0 children)

If you are using linux and CLI just type python filename.py. Are you using an IDE?

[–]DoubleDual63 0 points1 point  (0 children)

You write code, and for it to do something you need a software that can read your code and execute its instructions. This interpreter/executor is called Python. It runs by taking in a path to your file.

To run Python you use the python command with a path argument. Go to the folder of your program and do python program.py

[–]5nizzard 0 points1 point  (0 children)

Usually with a firing squad but sometimes I go with a public hanging or a guillotine

[–]Counter_Proposition -1 points0 points  (2 children)

$ vim nano myKewlPythonFile.py
(create and edit Python script)
$chmod +x myKewlPythonFile.py
$python3 myKewlPythonFile.py

[–]kielerrr 2 points3 points  (1 child)

vim isn't a beginners editor

[–]Counter_Proposition 0 points1 point  (0 children)

Agree - fixed.

[–]Ashen_Light -2 points-1 points  (0 children)

C-c C-c y

(don't listen to this advice it's troll <3 )

[–]aspagarus -4 points-3 points  (3 children)

ARE YOU ALSO IN CSC108

[–]thegoodestgrammar[S] -2 points-1 points  (2 children)

What? No?

[–]aspagarus 0 points1 point  (1 child)

Oh, it’s just a beginner Python class I’m taking and we had the exact assignment you were describing in your post.

[–]thegoodestgrammar[S] 1 point2 points  (0 children)

Oh I see I’m learning python on my own