Python and Atom


#1

Hello,

Newb here. I am starting off learning python using Atom as the default app. i was following a tutorial where the guy did this:

2+2

He would press enter and it would output

4

I tried that on Atom, and nothing happened. I have the scripts package installed, as well as linter, autocomplete, etc. Why doesn’t it work that way? Additionally, when I type:

2+2 and hit run, nothing shows up in the python console at the bottom unless I type print(2+2) and hit run. is there a way to do this without having to do print?

Thank you. Please be patient with me haha.


#2

In general, Atom is not designed to run code. There are packages which add support, such as script, but it doesn’t accept user input (e.g., from the Python input function). There are probably other packages that can handle it properly, but I don’t know any off the top of my head.

It sounds like this tutorial was using the interactive Python interpreter, invoked on the command line with python (or python2 / python3 to specify a version). The command line varies based on OS; for macOS, you can get one with the Terminal app.

You can also add a cross platform terminal interface to Atom with packages such as termination (this is what I’m currently using). There are other packages that provide a terminal, but it shouldn’t really matter what you use.

In this terminal you can run the command python and it will come up with something like this

13:12 $ python
Python 2.7.15+ (default, Oct  2 2018, 22:12:08)
[GCC 8.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

This is likely where the tutorial executed 2+2 to print 4

Note this is the interactive shell; it’s not how you would normally execute Python scripts. Normally you would run python filename.py or similar in the command line (not in the Python interpreter; press ctrl-d or execute the exit() function to go back to the normal command line). It will then read and execute the Python in the file filename.py. When done this way, it will only print out what you tell it too with print(), or a stack trace if it errors.

is there a way to do this without having to do print?

For a script in a file, no. And you wouldn’t want it to for one of any non trivial size. There are alternative options though; the hydrogen package allows you to execute and see values directly in the editor, and a Jupyter notebook opens up a local webpage interface to run code in blocks, with it printing the value of the last variable in each block. Both of these need the IPython kernel to be installed (though I don’t know if this is separately to the Jupyter Notebook).

If you want the notebook, see the installation docs. The easiest way to get it going is to install Anaconda, which has a GUI tool and comes with a lot of other Python stuff. The way I like it (and did just now) is to use pip3, the Python 3 package manager, to install using pip3 install jupyter in the command line. You will likely need Python 3 to be installed; check this with python3 --version in the command line. If you don’t have something and have trouble installing these things, you’re better off finding a dedicated Anaconda / Python forum for assistance (and be sure to tell them your OS).

Once Jupyter is installed, you can run jupyter notebook in the command line to open up the notebook manager.


#3

Thank you so much for such a detailed reply! What would you recommend then for someone who is a beginner who wants to learn python. I was really put off by the complexity of all of this. Is there a program that I can learn python in and have it work like it would in a real world job or profession? I found out the the guy in the tutorial was using IDLE. What do you think?


#4

I recommend this online tutorial. No hassle to get going, it gives you all the information you need.

One caveat is the course is for Python 2 (Python 3 is a paid course), but it notes the important differences (which is basically how to call print and how the input functions work). Most everything else you will learn is identical.

I can’t really explain setting up a local Python environment any better; look up Anaconda and some tutorials on how to install it.

I also highly recommend getting familiar with the command line; a lot of things (like understanding documentation and installation steps) are made easier when you’re familiar with common commands. You will also be able to fix issues better when the “magic” GUI tool you were using breaks. The site I mentioned above also provides a tutorial for that, but it only applies to macOS or Linux. I don’t know the Windows command prompt language.


#5

I feel like this is an important point and I want to talk a bit about the state of the software and the complexity of getting into it. It may sound Panglossian, but it’s easier now than it ever has been and you should view any learning experiences in your way as opportunities to enhance your skills. First I’ll talk about the easiest way to get started, then I’ll go into excessive detail about why you should be glad to tackle the challenges you face.

The Python terminal that @Aerijo mentioned is an example of a Read-Eval-Print Loop (REPL), which is a program designed for quick calculations or rapid prototyping. By far the fastest way to get started with a new language is to use an online REPL like the one at https://repl.it/languages/python3. When you learn on that site, you have the incalculably valuable ability to save your code and immediately share it with whomever you’re talking to on a forum like here or StackExchange. It is so good for collaboration, and I use it all the time when I want to develop new functions where I’m not sure exactly how they need to flow since the time to find and fix bugs is much shorter than if you write every new function in your project.

IDLE is good as a dedicated IDE already configured for Python. If you don’t buy any of the argument that follows, go ahead and use IDLE, but I don’t think it’s worth it when an online REPL is faster and more seamless and a language-agnostic editor like Atom has more potential uses.

You’re setting out with the goal of learning Python. That’s laudable, but it should not be your destination. Your destination should be a project that you want to build (if you don’t know what the project is yet or how to make it happen, you should still have a practical objective as the landmark you’re shooting for; perhaps that’s to land a job, but in order to land a job you’ll have to complete personal projects and build a portfolio, so it’s best to start small). The language you’re learning is just a step. You could do the exact same thing in several languages with only minor differences in outcome, and if you keep at this you’ll probably have to diversify. With that in mind, you want to onboard yourself (since you’re following tutorials and looking for editors, I’m guessing you don’t have a course at an educational institution) with an education that is broad enough to develop skills you expect to use in the course of your projects or job search.

If you stick with Atom and learn to make it work for you, you will develop so much knowledge about how computers work that will benefit you in different Python projects. The first is about how programs can communicate with one another through the system shell. Every Atom package that runs your code, and every IDE that has it built in (like Jupyter and IDLE), does exactly the same thing: they invoke the Python interpreter and pass along either your code or the file containing your code (the difference determines whether you need to save before running to see your changes). Here’s one place where you need to be careful: some of these programs bundle an interpreter with them, and sometimes they rely on one you have installed. It’s possible (and likely on some OSes) to have multiple versions that you use, so you have to be aware of when you’re invoking Python 2 versus Python 3. Now, this is some of the complexity you were worrying about, but trust me, once you’ve been looking at them for a few months the version numbers will be meaningful and you should know which ones belong to which projects (it’s unlikely, but possible, that you’ll have to use Python 2).

Using Atom to run your Python code is as easy as opening a command line window and typing python myfile.py. It requires exactly the same knowledge and skill, in that it first needs for you to install Python on your computer. Once you’ve done that, you have to select a package you want to use. They give you different approaches based on your workflow needs; script provides a one-click file runner that’s configurable and has settings that satisfy most needs, but isn’t very smart; termination gives you a terminal with full command line access and the most flexibility; and process-palette allows you to define complex command strings and bind them to Atom-internal commands that you can then activate from a keybinding or toolbar button. Each of these packages have “competitors” that offer variations on the same themes, but I won’t list all of them. I heavily endorse process-palette as the most powerful option, which will let you automate just about anything, especially once you learn to write more complex Python scripts (want to scrape text from a web site and drop it in a new tab in Atom ready to be edited?) and add new tools to your repertoire.

You just want to learn Python now, but once you get past the basic instructions and Exercism exercises, you’ll want to learn some of the many tools that are available for data science, machine learning, scaffolding, running web servers, or DIY IoT projects. When you do broaden your horizons, you’ll have to perform the same kind of broad reading about how all of the different things operate that you have to perform when you’re checking out new Atom packages. If you get comfortable with command-line instructions and command-line analogues now, it will be less uncomfortable later when a tool asks you to do some CLI setup. Right now when you don’t know anything is the perfect time to be broad and undiscriminating about what you learn. When you know your way around the basics of the language and want to develop specific skills, you won’t want to spend time and effort on basics that you didn’t learn because they weren’t part of the language you sought to acquire.

Complexity can be your friend. The complexity in using Atom is part of the draw for people like me. If you let it, it will teach you so many things that the tutorials don’t. If you want to make a career out of software development (or even just writing scripts on the side for your main job), that kind of expansive knowledge will be more valuable to you than having a perfect knowledge of the syntax of any one language.


#6

Wow. Ok this gave me more motivation to try it out. I’ve always wanted to dive into the realm of CS. I really liked the idea of it since I was surrounded by computers and tech as a child(escapist).

I thought I’d wait till next week to get back into it but I’ll definitely give it another try tonight. It’s just that the interfaces and the features were so complicated and I wanted something that wasn’t just IDLE…that I could use for other things too. I like to find something that fits me and stick with it and I guess I got pretty frustrated when it didn’t work out. So this is definitely a learning point!

I’ll break down what you and everyone else has said into more depth when I get home from work tonight. If I have any more questions pertaining to these messages I’ll ask them here as long as it is relevant. Thank you so much!


#7

This forum isn’t super-strict about remaining on topic (it would be futile anyway, one or two learning threads won’t constitute meaningful clutter, and TBQH this forum would be a lot less interesting without the people who come in with a complex problem that stretches beyond the editor) and both Atom and Python have the potential to empower one another, so it’s appropriate to cover the general computer science topics that they share and how anyone who is dogged enough and willing to learn new things can use high-level programming languages to achieve substantial benefits without having to be an expert or a career developer, using exclusively free software (not counting OSes).

Atom is exciting to me because it’s basically a web app running in a Chrome tab, which means that all of the code is in JavaScript, which I can edit because I’ve whipped together a handful of web pages. Here is software that gives me just about complete freedom to do what I want, and usually what I want has already been done by the developers or a package maker. Then it’s just a question of finding the code to make Atom dance or figuring it out myself. Some projects are too big or complex for me to handle without getting frustrated and ruining the fun, but it’s all accessible to me as someone who is neurologically cursed to be interdisciplinary.