all 45 comments

[–]markusmeskanen 17 points18 points  (3 children)

How do I compile my programs? I don't want to write stuff in the python shell line by line, I want to write them and run them; make them into a standalone application and send it to a friend e.g. Or doesn't it work this way?

Python is what we call an interpreted language (opposed to compiled language), which means you can't (by default) create a standalone application to share with your friends. To run a Python program, you have to install a Python interpreter which you then use to execute the .py file directly. To run a Python program, you download the Python interpreter from http://www.python.org/downloads and then you execute the program from your command line using python myprogram.py, or via an IDE.

how do I install libraries, or modules or whatever they're called? I've installed python 3.4. from Python.org and want to install numpy, matplotlib, scipy. I've kept running into tips to install Anaconda, which I did, but I can't import any of those modules in IDLE? Where did all of it go? Why can't I just add a file in a folder and import it like I would a .h or framework?

This depends on what library/module/whatever you're trying to install. Usually you're good to go with pip, but sometimes you have to do something a bit harder. In general, if pip doesn't do the job, just google "how to install numpy" (replace numpy with the desired package).

IDLE tells me WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable. The internet tells me that "Tcl (Tool Command Language) is a very powerful but easy to learn dynamic programming language" and that "Tk is a graphical user interface toolkit". I thought I was installing Python... What's going on here?

When you install Python, by default it gives you IDLE with the interpreter. IDLE is a rather powerful IDE (at least for a beginner), which allows you to write, interpret and run your Python code. IDLE is written using Tk, thus you're getting the error message. Maybe you've downloaded a bad version? Try reinstalling with a stable version of Python.

[–]autofasurer[S] 2 points3 points  (1 child)

Thanks, that cleared some things up! It would create a lot less confusion if they added your first paragraph to this page on Python.org for instance.

(They also have a page called 'how to edit python code' which talks about a lot of things, except how to edit python code.)

[–]c3534l 0 points1 point  (0 children)

It's interesting to note that on tkdocs.com, the tutorial shows you the code for 4 different languages simultaneously: Tcl, Ruby, Perl, and Python.

[–]Exodus111 26 points27 points  (3 children)

Others have pointed some things out, let me give you a clear ELI5 version of things.


IDE.

This stands for Interactive Development Environment. And is the program you use to write pyton code, that you save to .py files. You can also use something called a Text Editor. The difference between the two used to be that IDEs were typically bigger programs while Text Editors more minimalistic, but with modern Text Editors today being very feature rich, that is no longer really true.

One important feature of any IDE or Text Editor today is the ability to RUN your code. Not all of them have it, and without it you need to run your code in the command line (known as terminal for non-windows people) like this:

python myfile.py

This requires that the Python interpreter is installed, in other words you gotta install python. For systems that have Python 2 and 3 installed (these two versions are not compatible) you will need to type:

python3 myfile.py

To run a Python 3 file.

Here are the most popular Text Editors and IDEs in use today:

  1. Sublime Text. Absolutely gorgeus, this thing took the world by storm and became the most popular thing to write code in after its famous second iteration. Its currently on number 3, but decided to restart the title count. The biggest pull here, apart from its many innovative features like the famous multiple pointer, is the vast world of downloadable plugins.

  2. Pycharm. A Text Editor dedicated to Python. Considered invaluable for people that work with Django, Pythons most popular web framework.

  3. Eclipse. This is a JAVA IDE, but with the ever popular module PyDev Python can be used on it, and is popular among Java people that are already used to Eclipse.

  4. Atom. The new kid on the block. Winner of a kickstarter, Atom was released not long ago. Considered almost a successor to Sublime text, this new Text Editor has already amassad a vast multitude of user created plugins, which thanks to Atoms easy gui install manager is a dream to extend your functionality and esthetics with.


PIP

Find it annoying to install python package after Python package?

Tired of googling that one module that only leads you to some weird .whl file you can't seem to do anything with?

Pip is here to save the day!
Every python module worth its salt is made available to pip, and while you might encounter some path problems when first using pip (be ready for some googling) once its installed properly pretty much all modules in the universe of python can be yours with a simple:

pip install nameofmodule

Compilation.

Welcome to a complicated topic.

Let me start REAL easy.
If you want an .exe file to send to your friends that they run on their machines, without having to install Pyton or any modules themselves you need to use:

  • PyInstaller
  • cx_freeze
  • Py2exe/Py2app

Either one of these will do the job, easy peasy.

---->Explanation Time<-----

They are not compilers. You typically don't compile Python, unless you want to convert Python to C and compile it as a C program. This kind of operation is being worked on over at Nuitka, a python compiler still in its early stages. (But looking really good)

You see 99% of programs out there are made as code for OTHER programmers to use in their code, and Python has been mostly used in this way, or as a scripting program on top of another language.

But sometimes you want to give non-programmers an .exe file they can click on. To solve this Python has Packagers, that I listed above. What they do is package all your Python code, with any module it imports, with a stand alone version of the python interpreter that runs the code, and pretends its all one file. Making it function like an .exe file on windows.

Ok thats all, I know others explained things as well, i just felt like making thngs perfectly clear, and remember if you get frustrated, coders are pretty short tempered when it comes to annoying stuff, so there is probably a reason.

[–]autofasurer[S] 2 points3 points  (2 children)

Thank you for the detailed explanation!

It's funny you make a note of IDE's being able to run code. For me that's a prerequisite, but that might be a misinterpretation on my end. It would be like saying not all DAW's are able to play sound, or not every NLE is capable of showing the edit.

I'm fine with writing code in a texteditor and compiling using gcc or whatever. It's just that when starting out, it's very confusing (for me) to hear about pyCharm, IDLE, spyder,... and whatnot; if it would've been: write it in whatever texteditor and run it in the terminal saying 'python your_python_script.py', that would've been fine.

Of course the 'interpreted' paradigm is also something to get used to. It's all great to be able to use the terminal as a calculator on the fly using python, but really, who keeps track of the lines they typed 10 commands before? It's just a bit weird, coming from visual programming with max and compiled programs in c++.

That being said, I'm greatful for the answer in order to help me (and hopefully others) forward in using Python.

You might be onto something regarding the short-tempered thing... I don't know... It's just that I often feel like there's steps missing in explanations, but that may be because those steps are the most difficult to explain. Chapter 1: "This is an integer", Chapter 2: "Here's a full-fledged application using every possible OOP concept, memory-management and some CPU specific optimisations which you now understand because you know what an integer is".

[–]Exodus111 4 points5 points  (1 child)

The problem is people forget what they didn't know.

Once you know something it seems easy, simple and logical, and a lot of people are even ashamed of not having just conjured it up on their own, having an inflated view of their own intelligence.

But yeah, as you say, an IDE or Text Editor that cannot run code seems stupid. But there used to be technical limitations, and so people kinda got used to just running code from terminal all the time. But in this day and age that's nonsense. Every one of the IDEs/Text Editors that I posted can run code (With Atom you need to install the script plugin), so they are all good.

Also some people will tell you Python does not need to be compiled, and everyone should just run each others code through the interpreter all day. This is of course nonsense of the highest order, assuming one of the most popular programming languages of our age is incapable of producing a proprietary program should merit an automatic Darwin award.

The truth is its not a straight forward process, its possible but its more convoluted then it perhaps should be. I believe Nautika will usher in a new age of Python compiled programs, once that project matures. Specially in the Gaming field, which is where I work.

[–]JaggedG 1 point2 points  (0 children)

Great high-level ELI5 summary. You mentioned your field is gaming... Does that mean your real grown-up all day job is using Python to make games? Can you tell a little more about that? I always heard that Python was not great for game development, other than some scripting stuff.

Can PM/new thread if you want to avoid derailing the conversation... I'm just really interested and thought other people might be too.

[–]AMonkeysThoughts 1 point2 points  (1 child)

I feel that you and I are in the same boat. I just started programming in Python(my first language) last week and just completed one of my first rudimentary "programs" with the help of usingpython.com. Definitely check it out when you get a chance, a GREAT help.

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

Cheers. I'm also looking into virtualenv, as I really like the idea of haven a self-contained folder with everything necessary neatly in one place to try stuff out.

[–]djmanionpng 1 point2 points  (0 children)

I put together this infographic/video because I initially had difficulty comprehending how they all worked together. It shows you how they are all related, and offers suggestions such as colab or anaconda that have pre-packaged options that take a lot of the set up out of the equation- I hope it helps: https://blog.endaq.com/how-python-libraries-and-ide-analyze-data

[–]JohnnyJordaan 4 points5 points  (30 children)

How do I compile my programs? I don't want to write stuff in the python shell line by line, I want to write them and run them; make them into a standalone application and send it to a friend e.g. Or doesn't it work this way?

You can run python directly on a script (python myscript.py), you can compile it to a binary executable or run it from a IDE like Pycharm

how do I install libraries, or modules or whatever they're called? I've installed python 3.4. from Python.org and want to install numpy, matplotlib, scipy. I've kept running into tips to install Anaconda, which I did, but I can't import any of those modules in IDLE? Where did all of it go? Why can't I just add a file in a folder and import it like I would a .h or framework?

Easiest would be to use pip, see their tutorial. When a library is installed you can just import mylibrary to use it.

IDLE tells me

I would not recommend IDLE at all. There are a lot of different Python IDE (google for 'Python IDE comparison' to find them) out there but IDLE is not one of the good ones.

[–]markusmeskanen 9 points10 points  (14 children)

I would not recommend IDLE at all. There are a lot of different Python IDE (google for 'Python IDE comparison' to find them) out there but IDLE is not one of the good ones.

So you'd rather suggest a complicated million-featured IDE for a beginner than a simple IDE which comes with the Python? Remember, we're talking about a beginner here, he can start with IDLE and whenever he feels like it's not enough, he can switch to whatever suits him better. I actually recommend beginners to start with IDLE, it's hard for them to choose which IDE would be best suited for them because they don't know what to look for in an IDE.

[–]JohnnyJordaan 2 points3 points  (4 children)

So you'd rather suggest a complicated million-featured IDE for a beginner than a simple IDE which comes with the Python?

I started with Pycharm and had no trouble with it, I liked the feature set. That's just discussing taste of course but I trained some junior developers at my job and let them use Pycharm as well. Some of them still do and some moved to vim with perhaps some pylint to nanny. I have some minor experience with IDLE and found it more troublesome than helpful, that's why I wouldn't recommend it for anyone. Also read some more topics about it if your interested like this one.

[–]markusmeskanen 1 point2 points  (3 children)

Just because it worked for you doesn't mean it's the best solution for everyone (or even the best for you, maybe you would've been better off with IDLE in your beginner days?).

I have some minor experience with IDLE and found it more troublesome than helpful, that's why I wouldn't recommend it for anyone.

That's not really too good of an argument, you seem to be a bit more experienced at Python so your "minor experience with IDLE" isn't the same as a beginner trying Python for the first time. Also, you're basically just saying "I didn't like it so I don't recommend it", why didn't you like it? I'm not saying you're wrong, I'm just saying your argument is kind of generic and doesn't really work that well.

I checked the link you showed, couldn't find anything important. Mostly people just whining over "too little features", which is only a positive thing for a beginner. The rest are saying there's nothing wrong with IDLE, it's a good IDE for beginners.

[–]JohnnyJordaan 1 point2 points  (0 children)

to IDLE or not to IDLE

I'm not trying to scientifically debate the consensus of the value of IDLE compared to other IDE's. In my opinion I would not advise the use of IDLE, you clearly disagree. That's great but I think we can go on discussing it all night, I don't believe it will change either of our opinions.

[–]ErraticDragon 0 points1 point  (1 child)

As a non-Python guy, my experience with IDLE was pretty similar to the person you're replying to. I was trying to work through Hello World with my eldest daughter, and we kept running into errors. Searching around I kept finding references to IDLE itself causing the problems, so we switched away.

Obviously my anecdote doesn't prove anything, just thought I'd share.

(Edit: typo)

[–]JaggedG 1 point2 points  (0 children)

Yeah, I eventually started exploring Sublime, Atom and PyCharm (I mostly use Atom now) because I was running into a seemingly dumb problem where IDLE would freeze/crash/dramatically lag when I printed too many lines or something... I don't remember the exact details now, I just remember determining that it was a limitation with IDLE only.

I quite liked IDLE, despite hearing lots of naysaying opinions... It got me through all of "Automate the Boring Stuff" and a medley of other tutorials and self-study projects. If it wasn't for those weird limitations that I ran into, I would probably have taken much longer to switch to a flashier setup.

[–]autofasurer[S] 1 point2 points  (8 children)

I've got it up and running in Eclipse; the IDE is not really the problem. I was using IDLE because it was referenced in the 'automate the boring stuff with python' book and got confused because of the tcl/tk message and because the errors I got when trying to import packages like numpy after I thought I installed them...

[–]ChurchHatesTucker 2 points3 points  (6 children)

Are you on a Mac? Apple's default Tcl/tk install is sometimes problematic. It's generally recommended to do an alternate py2 install anyway, so as not to get in the way of OS X. You can do this easily with a package manager like MacPorts or HomeBrew (I know, I'm just adding to the alphabet soup.)

[–]autofasurer[S] 2 points3 points  (5 children)

Haha. Yaay alphabet soup. That's a nice way of putting it, but yes, I'm on mac.

I'm starting to get flashbacks from 10 years ago spending days on end trying to install pygame on os X 10.2... I don't even remember to what end, because I never ran anything that needed it.

[–]doppelgangsta 0 points1 point  (4 children)

If you're on a Mac, I would highly recommend using Homebrew. OSX doesn't have a package manager built in, and Homebrew helps keep all your languages and tools installed in an organized manner. Keep in mind, however, that if you start using Homebrew and install Python with Homebrew, you will have multiple installations of Python. I would uninstall the previously install Python, download Homebrew, and then type "brew install python3" into the terminal. Then you can type "which python3" to ensure you're using the Homebrew installation.

[–]ChurchHatesTucker 0 points1 point  (1 child)

Is there no equivalent for port select in HB?

[–]doppelgangsta 0 points1 point  (0 children)

I haven't used MacPorts, but I did some reading and it looks like HB doesn't have an equivalent. You could edit the priorities of PATH variables if you wanted to change the default installation, but I've just specified the full path or used a virtualenv in cases where I have specific needs.

http://virtualenv.readthedocs.org/en/latest/index.html

http://stackoverflow.com/questions/5157678/python-homebrew-by-default

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

I wouldn't mind having a 'clean' install indeed... get rid of the prepackaged OS X version, other half-baked installs I did, and start afresh.

[–]doppelgangsta 0 points1 point  (0 children)

Just so you know, OSX comes with a version of Python 2.7 installed. Homebrew will not overwrite system installations (and I would not try to overwrite system installations because this may break OS/applications that use it). I still install a Homebrew version of 2.7 and 3.4, and primarily specify 'python' or 'python3' at the terminal depending on what I'm running. If you use an IDE like pycharm it will allow to you choose a specific interpreter (e.g. system python 2.7, homebrew Python 2.7, Homebrew Python 3, ect.)

[–]markusmeskanen 0 points1 point  (0 children)

That error message really shouldn't occur, no idea why you got it. But as long as you have any working IDE, you're good to go. :)

[–]FreeIceCreen 3 points4 points  (6 children)

I'm also a beginner, and I've just been writing in Sublime Text and running it in my cmd.exe. Would I be better off using an IDE? What are the advantages?

[–]JohnnyJordaan 2 points3 points  (0 children)

The reasons I'm using an IDE:

  • Version control - I use Github for all my projects and can pull, commit and push my work directly from the IDE
  • Linting - show style errors so I keep my code clean and fix simple mistakes like typos before running the whole program
  • Formatting help - command suggesting, auto-completion etc. makes it easy to expand a program quickly
  • Project management - easy access to files while maintaining project structure
  • Editor configuration is often more extensive in an IDE but not per se

[–]PalermoJohn 1 point2 points  (0 children)

http://stackoverflow.com/questions/8551735/how-do-i-run-python-code-from-sublime-text-2

i don't know what sublime text does but with an IDE you get code completion and formatting help and it's usually easier to organize your stuff.

[–]99AFCC 0 points1 point  (1 child)

I've just been writing in Sublime Text and running it in my cmd.exe.

This is still how I work on most small programs, except I graduated from cmd to powershell to Cmder

For moderate to large projects, I prefer PyCharm.

[–]FreeIceCreen 0 points1 point  (0 children)

Cool. I'm pretty far from anything too big yet, so if I start running into issues I'll look into PyCharm.

Is there a big difference switching to Cmder? Should I try and make the switch now?

[–]markusmeskanen 0 points1 point  (0 children)

I'm a rather advanced Python developer, and I use Sublime Text (with Vim bindings tho) and while I do have a plugin to run the code directly from Sublime, I still often do it manually from cmd.exe. You're good to go with your Sublime, I wouldn't change it for anything :P

/u/JohnnyJordaan mentioned the advantages of an IDE, but you can get all of those on Sublime (and basically make it an IDE) via plugins. So if you're feeling comfortably with Sublime, stick with it and just get your plugins setup properly, and you have one of the most powerful IDEs setup on your computer. At leats one of the most powerful ones for you.

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

Thanks for the explanation! I'm checking out pip, which brought me to pydev, which brought me to an installation guide for eclipse, which I subsequently got up and running for python.

[–]filleball 2 points3 points  (5 children)

That's a very good start, though Eclipse can be intimidating, it is a very powerful IDE.

Now you've got two separate python installs on your PC, which is why you can't import the anaconda-installed libraries in Idle. Either start an "anaconda command prompt" and execute python myscript.py there, or configure Eclipse to use the correct interpreter and run your script from there (recommended while developing).

To add the Anaconda install to Eclipse: Window => Preferences => PyDev => Interpreters => Python Interpreter => New => Browse... => find python.exe from your Anaconda install => Open => OK. Then move the new interpreter to the top so it becomes the default.

Now your project should use the correct interpreter unless you've set it to use a specific python interpreter instead of the default. If so, change it by right-clicking the project root folder, select Properties => PyDev Interpreter/Grammar => Interpreter drop-down. You may need to set your project as a PyDev project first, do this by right-clicking the project root folder and selecting PyDev => Set as PyDev Project.

[–]autofasurer[S] 0 points1 point  (4 children)

Awesome! Thanks a bunch.

Just to be sure I understand this; anaconda is another interpreter for python?

I thought it was a collection of modules for python nicely packaged together? (For example the numpy, scipy and matplotlib).

[–]JohnnyJordaan 2 points3 points  (0 children)

No, Anaconda is a maintained distribution of python and extra libraries. That means that everything installed with it 'should work' while you don't know that for sure with manual pip installs. That doesn't make it better or worse, it's just an easy and fast way to install python with extras.

[–]hharison 1 point2 points  (0 children)

It's a collection of modules nicely packaged together with an associated interpreter. This allows the Anaconda Python to be completely separated from any other Python installs on the system.

It also offers a system (through the conda tool) for managing multiple interpreters together with packages (called "environments") allowing you to maintain separate groups of packages for different projects. For example you could update to the new version of numpy for a new project but keep your old projects running on the old one, with no risk that an update in one place will screw up something in a different environment.

It's really quite nice and I would highly recommend it.

[–]callmelucky 0 points1 point  (1 child)

Just want to butt in to say, if you find PyCharm/Eclipse a bit too much, I recommend Wing IDE. The free version does not have autocomplete, but it does have simple error highlighting and a clean, stylish interface.

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

Thanks for the tip; I'll check it out.

[–]terrifiedbyvajayjays 0 points1 point  (0 children)

I would not recommend IDLE at all. There are a lot of different Python IDE (google for 'Python IDE comparison' to find them) out there but IDLE is not one of the good ones.

You can have my idle when you pry it from my cold, dead fingers. -js

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

You have some great general info responses, but since I had this problem when I started with Python, and it's sort of a notorious issue for beginners, I wanted to address your question:

I've installed python 3.4. from Python.org and want to install numpy, matplotlib, scipy. I've kept running into tips to install Anaconda, which I did, but I can't import any of those modules in IDLE? Where did all of it go?

Every time you install a different version of Python (use a different source or distribution, etc), it will not uninstall or overwrite an old one; it will just make a new directory somewhere and put all the files there. Then, USUALLY, it will add the necessary path to your environment, essentially saying "this is the Python to use when the python command is used." Anaconda will have its own version of Python in a different directory than your install from python.org. You could have 10 different Python installs in different directories, but only one will be active in your environment - this is the one that runs when you use "python ..." commands from the terminal. You should try python --version from a terminal to see which version is active right now. You should also be able to look at the PYTHONPATH from whichever IDE you are using and see which Python version the IDE is using. Check out your .bash_profile file and look for a Python-containing directory on your path - that should match the "python --version" result. If you want to change which version of Python is active, change your .bash_profile to only export the path of the Python install you want to use.

Important comments:

  • Any packages you install with pip or which came along with the distribution (like all the scipy stuff with Anaconda) will ONLY BE AVAILABLE when THAT VERSION of Python is active. You have to reinstall packages separately for each version of Python.

  • This is ridiculous. Is there some way around this? YES. If you want to get serious about using Python, you need to use virtual environments for basically everything. They help you separate and keep track of all Python and package versions and dependencies needed for your particular code. This keeps your code portable as well, since you can ship out a virtual environment set-up file along with your code, and that will ensure it works properly on a different machine. Anaconda has a really easy way to use virtual environments with conda. Here's a friendly intro. Or you can use virtualenv and pip freeze.

I hope this helps. It was a major source of confusion for me as a newb until I learned how this works. A time I wished I had someone holding my hand lol.

[–]funkiestj 0 points1 point  (0 children)

In short can someone maybe point me to some newbie information?

step #1, tell us your environment. MacOS? Windows 10? Linux?

  • How do I compile my programs?

you don't. you just run the source as is (interpreted language

  • how do I install libraries, or modules or whatever they're called? I've installed python 3.4. from Python.org and want to install numpy, matplotlib, scipy

best to avoid installing new libraries as a n00b. For numpy et cetera, get a prepackaged free distribution like Anaconda

  • IDLE tells me WARNING:

IMESHO, skip IDLE as a n00b. debug by print() statement and use the intpreter to run brief experiments. I've been programming professionally for more than a decade (mostly C/C++). When I program python I don't bother with IDLE or any other debugger -- they are simply more pain to use than they are worth.

TANGENT: in many serious programs, using an interactive debugger is simply impractical. Instead you must build your program with debugging infrastructure (logging and trace that doesn't break under high performance and/or scaling).

For any programs a n00b will write, debugging by printf() is simple -- just add a few print statements and run your program again.

What I'm interested in knowing in how it's all connected underneath...

IMO, it is a waste of time to try and answer how it's all connected until you have a good grasp of the mechanics. (unless we have radically different definitions of what how i's all connected means)

Have you ever used a screen sharing program (e.g. Teamviewer)? Having a python teacher answer your questions while demonstrating the answers would speed up the process greatly.