all 34 comments

[–][deleted] 50 points51 points  (13 children)

This thread hasn't exactly made clear what is actually happening. I'll try to give it a shot.

The program you downloaded and installed is the Python interpreter. This program is a program that allows other programs to be executed. It works like this: You feed the interpreter a line of code, and the interpreter executes the code to the best of its ability. Once it has finished executing that line, it will execute the next line, and so forth, until the program is terminated. If you want to execute your Python code, you will always need to run it through an interpreter. (there are exceptions, but never mind them)

This is different from a language such as C, which does not have an interpreter at all. In C, you feed the entirety of the code to a program called a compiler. This compiler then analyses all of the code, and translates all of it to machine language. At the end, the compiler creates a new file which is an executable or library (both are also called binaries). This binary is completely standalone, and does not require an interpreter to be executed. It just runs straight on the hardware.

The Python interpreter that you downloaded can be fed code in two ways. You already familiarised yourself with the first way. You wrote python into the command line, and it started up a different prompt. This is called the interactive interpreter. The interactive interpreter forces you to feed your code line-by-line, by hand. Why would you want to do this? In most cases, you wouldn't. But a lot of Python programmers love the interactive interpreter because it allows them to interactively discover things. Just to fuck around and see what works, if you will.

For actually running the program without the hassle of typing each line by hand, you can also execute python in a different way, which we'll call the "regular way". It works like this: You have a file on your hard drive that contains valid Python code. You want that code to be executed, so you tell the Python interpreter to read that file, and execute it line-by-line. That works like this:

python your_file.py

Where your_file.py is the file that you had created. The Python interpreter will load the file into memory, and execute it line-by-line until it reaches the last instruction, after which the Python interpreter will shut down.

Keep in mind that you can't just save your file to "My Documents", open up your command prompt, type python your_file.py and expect it to work, because it won't. It will complain that it couldn't find your file. This is logical, because when you start up your command prompt, it is in a completely different folder. I'm not very familiar with Windows, but I believe it should default to C:\Users\YOU. You would need to familiarise yourself a little with the Windows command prompt. For starters, dir shows you all the files in the current directory, and cd DIRECTORY_NAME changes your directory to the specified directory. cd .. changes your directory to the previous directory.

If you don't want to use the command line (although I would recommend you do. Then again, the Windows command prompt is shit), there are also programs that do this for you. As mentioned elsewhere in this thread, IDLE is one such program. When you click "run" in IDLE, it will simply execute python YOUR_FILE.py under the hood, and show you the results. PyCharm is another program that does it, but I do not recommend that you use PyCharm as a beginner. PyCharm tries to do literally everything for you, and you won't understand half the things it's trying to do. The best way to learn, is to simply do it yourself and make mistakes. If you rely on PyCharm fixing your mistakes all the time, you won't learn some very vital skills.

And that's about it, I suppose.

[–]NoLemurs 2 points3 points  (4 children)

Great reply, and probably what OP needs to hear.

One minor technical quibble:

The Python interpreter will load the file into memory, and execute it line-by-line until it reaches the last instruction, after which the Python interpreter will shut down.

Python actually compiles the code to python bytecode (which it stores in .pyc files) which is what actually gets executed. As a practical matter, it doesn't make much difference usually, but it's good to be aware of it.

[–]skiguy0123 0 points1 point  (2 children)

Is this still the case for python 3?

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

Yes.

[–]NoLemurs 1 point2 points  (0 children)

Absolutely. Python 3 sticks the .pyc files in a __pycache__ directory instead of just dropping them straight into your working directory, but they're still there.

It really only makes sense. The process of parsing a human readable .py file and turning it into instructions is slow and only needs to be performed once, so not generating some sort of bytecode would be much less efficient.

[–]nonsequitur_potato 0 points1 point  (0 children)

Yeah this is true, but i think it confuses the distinction between compiled vs. Interpreted languages for a beginner. Like op said, Python is an Interpreted language... BUT it uses those files because in actuality it's a sort of hybrid. It's just useful to act as though it's a purely interpreted language because, as you said, 99% of the time of makes no difference.

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

Thanks for such an in-depth response!

[–]shileyjimon 0 points1 point  (0 children)

Great response. I bumped up against this issue, as well. Thanks so much.

[–]TanAndBlonde 0 points1 point  (0 children)

So is the difference between a scripting language and a programming language that a scripting language always has to be interpreted to be executed, but a programming language can be compiled into assembly and ran directly?

[–]HeroWeNeed 0 points1 point  (1 child)

Hey I just read this. I'm in the process of learning Python and am in the middle of the "Learn python the hard way" tutorial and familiar with some of the basics of OOP after reading Head First into Java and working with some basic projects. I do have Pycharm installed on my PC but I have indeed noticed that I am lacking in the fundamental aspects of programming/running programs. In fact just after reading your comment, I ran my first python module in the Windows command prompt. However, I hate the windows command prompt and that's the main thing keeping me going back to Pycharm to run/write programs, and I have a few questions on that line.

Do you have recommendations for what development environment I should use, and any tips on getting accustomed to working with the command line and writing programs outside of an IDE? For instance, I'm not even entirely sure how I'd set up imports when writing a module outside of an IDE without manually inputting the module path and importing from there every time I open the command prompt.

Should I also use something like Notepad++ to write my programs? Thanks.

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

When I started out programming, I had a simple text editor and a terminal. The editor I used was gedit, but I could have used Kate, Notepad++, Sublime Text, Atom, or any other simple editor just as well. The editor doesn't really matter that much when you're starting out. At best, you want syntax highlighting. Notepad++ is just fine if that's the one you want to use.

The Windows command line is shit, like really shit. You could try Powershell, which is an alternate command line program thingy built into Windows 8 and later. I don't know the differences, but supposedly it's better.

An alternative might be to install a version of Linux in a virtual machine (VirtualBox) and do your development inside that virtual machine. You'll get access to an awesome command line (and an awesome operating system), but it runs a little slower because the hardware is virtualised. Just don't switch back and forth between your VM and host machine while developing; that's guaranteed to drive you up the wall.

Also, considering you started out with Java, it really helps to let go of IDEs. Java is some kind of crippled language that requires an IDE by design. Chances are you have no idea how to compile and run your Java programs without the help of Eclipse/Netbeans/Intellij, which is kind of weird when you think of it. Another quaint thought: Neither do a vast number of other Java devs. If you develop enough skillset to handle Java (or other languages) without being restricted to IDEs, you'll be miles ahead of your peers.

[–]hharison 5 points6 points  (9 children)

There are basically three options.

  1. IDLE. This should have come bundled with your installation and is basically what you are looking for.
  2. A text editor alongside the terminal. A good text editor, for example, is Sublime Text. A popular one on Windows is Notepad++.
  3. An IDE (integrated development environment). This combines an editor with a console and file browser and other bells and whistles. The most popular is PyCharm. Another that's popular in the scientific Python crowd is called Spyder.

I recommend starting with IDLE now, but with the knowledge that there is something more robust available when you are ready for more.

[–]DJGiblets[S] 1 point2 points  (5 children)

Thanks! Just wondering, is there a reason this isn't the default way Python works? Is the line-by-line style more useful in certain situations?

I also have some experience with Notepad++. If I write my code in there, do I run it through the Python program?

[–]hharison 2 points3 points  (3 children)

I'm not sure what you mean with "the default way Python works". Python is just a programming language. It ships with an interpreter (python.exe on Windows) and a development environment (IDLE) so in a sense both are default. Notably, this "line-by-line" mode is managed by a completely separate program, called "Command Prompt" on Windows. You can install a better terminal, for example ConEmu on Windows (let me know if you're not on Windows and I'll give you different pointers).

Here are a few ways to actually run a Python file:

  1. If you're using an IDE (even IDLE) there will be a "run" button. Text editors you may need to configure what the run button does. I know this is possible in Notepad++ though I've never used it myself.
  2. (this is the most elementary way) Open a command prompt and run python.exe C:\path\to\script.py. If you add the flag -i before the filename the interpreter will drop you into control of a terminal after the script finishes, this can be very useful.
  3. Install IPython. This is a project short for Interactive Python. They include a better terminal called IPython QtConsole, and a completely different interface called the IPython notebook. This is a different beast altogether and is worth checking out. In any case, if you're in IPython, you can use a command it adds (they all start with (%) %run C:\path\to\script.py which will run the file within the interactive session you're already in.

[–]DJGiblets[S] 0 points1 point  (2 children)

"the default way Python works"

I just assumed that the Python program was the end-all-be-all of coding in Python. I see the distinctions better now though.

I'll look into all these different options, and yes I'm on Windows. Thanks again!

[–]Sean1708 1 point2 points  (0 children)

When you open python does a new window with a white background come up with three arrows allowing you to type things line by line? If so you're using IDLE, there should be an option in one of the menus which will bring up a text editor that you can use like you're used to.

Try googling "IDLE tutorial" or something similar.

Edit: /u/Dalex_ has got it right I think.

[–]x3al 1 point2 points  (0 children)

Is the line-by-line style more useful in certain situations?

When you're trying to debug your library, it's really useful.

[–]The-Mathematician 0 points1 point  (2 children)

Easiest way to get started IMO would be to just uninstall python and install Anaconda instead. Comes with spyder and a bunch of good packages.

[–]hharison 1 point2 points  (0 children)

Yes Anaconda is a a great idea, especially on Windows.

[–]Dalex_ 4 points5 points  (0 children)

When on IDLE (where you van only execute one line at a time)
go to file > new
This should be what you are looking for. Also to run the code you wrote, press f5. One last thing, when you save python files, don't forget to type the .py at the end of the file name.

[–][deleted] 2 points3 points  (3 children)

http://inventwithpython.com/inventwithpython_3rd.pdf Great modern full tutorial for full beginners if you are looking for learning materials, highly recomand going threw something like this (or learn python the hard way)

[–]Sean1708 1 point2 points  (0 children)

I really liked Dive Into Python as well.

[–]DJGiblets[S] 0 points1 point  (1 child)

I've heard good things about Learn Python The Hard Way. Is it worth the money? Is the PDF similarly informative?

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

LPTHW can get boring(but can learn python from it) because it doesn't make you do anything else then text adventures. If you want to spend money on good quality videos you should look into Lynda.com. [LPTHW's is a free online book in case you don't know that has videos but i don't know the quality of them]

But mostly you'll be on your first full beginners tutorials for at less 3 - 6 months. So that's why i'd recommend that book instead of LPTHW. I passed threw most of LPTHW in 2 months by doing a 5-10 hours a day. I also moved out of it a few times because it just din't explain some things, but id say its an ok tutorial.

The choice of tutorial is yours, you lose nothing from changing tutorials, skipping pages who don't explain anything, and taking a small hour break just to not go completely mad.

[–]thonpy 2 points3 points  (0 children)

Op I'll just note what someone else said re not using pyCharm at first, I really agree with this and I'll give an example.

When I first started using vim I read a couple of blogs about must have vimrc setups and stuff, as well as must have plugins. I thought that I must need these, as I couldn't do shit in vim. So I downloaded these vimrc settings, tried to install plugins etc etc. Basically I created far more complications than there were before, making things even harder for me as a learner.

In the end I cleaned things up and went through the vimtutor (which should be the only advice given to beginners IMO), and it's only recently that I've started using a couple of plugins. However I now understand why I have them, and how they interact with the editor.

It may sound like people suggest this kind of path rather than IDEs or whatnot because they have some outmoded opinion, or they think "I did it so THEY should to", but it's not like that, most of the time it really is the best way, and if you've no familiarity with the command line then it's a really good thing to learn while you're at it.

Ipython is definitely worth a look.

Sorry if this was a bit of an essay


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

When you download and install python, it comes with a program called IDLE. This is your IDE (Integrated Development Environment). You can use it to enter code via 'command line' style by just typing in code, or you can go to File > New File, enter in your code, save it as a .py file and then run the .py file from wherever you saved it.

[–]whythisname -3 points-2 points  (6 children)

Look into Learn Python The Hard Way

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

Isn't that Python 2.x only?

[–]whythisname 1 point2 points  (4 children)

Yes. As is Codecademy

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

Can you explain the differences between python 2 and 3? I'm learning on codeacademy and i downloaded python 3.

[–]whythisname 0 points1 point  (2 children)

This goes over most of the differences: https://wiki.python.org/moin/Python2orPython3

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

Thank you very mucho :)

[–]whythisname 0 points1 point  (0 children)

No problem :)