Atom-script package cannot detect defined variables


#1

When I run scripts in atom via atom-script, variables can be defined but are not detected by the runner.

test.py:

x=1
x

NameError: name ‘x’ is not defined
[Finished in 0.061s]

Applescript:

set myVar to "abc"
display dialog myVar

execution error: The variable myVar is not defined. (-2753)

What went wrong? Tried starting atom from the Terminal but nothing changed.


#2

script has no ability to detect variables and no awareness of anything going on inside the script except for what is output to it. It submits your code to the relevant interpreter, which runs the code exactly as provided. Mind posting a screenshot of your Python file? If your code is correct, then something is going wrong about how it’s being transmitted to the interpreter, but since the results indicate that it has run successfully and the package works happily for others, it’s necessary to eliminate the code as an issue first.


#3

My test code above contains just 2 lines:

x=1
x

python3 gave the desired result, but not with script.


#4

I suspect this has something to do with environment variables that can be accessed via script


#5

I asked for a screenshot, not copying and pasting what was in your first post. I cannot help you figure out what’s going wrong unless you provide me with exactly the information I ask for.

python3 gave the desired result, but not with script.

Notice how you’re not actually testing the file in question. You’re just running the code in the Python REPL. In order to find out if your file works with python3, you need to use the command python3 test.py from the command line. You should also try this from within Atom using a package such as process-palette or termination, your choice, or running a command directly through Atom’s dev tools using the API as I explained yesterday in another thread.


#6

Sorry about that, here is the requested screen shot.

python3 test.py doesn’t yield any output, either.


#7

The above code produced the desired output in terminal, but not within atom.
$ python3 test.py
1


#8

I am also able to run the test code via termination on Atom.

python3 and ipython are both working normally in there.


#9

The default script behavior is to use the python command, not python3. Have you configured the package to use python3?


#10

I have a .bash_alias script that aliases python to python3.
Also, I’ve added these lines to my config.cson:

runner:
  scopes:
    osascript: "/usr/bin/osascript"
    python: "/usr/local/bin/python3"


#11

Just found something interesting:

atom-script actually gave the expected response when the python script is run as a whole.

It just has no access to previously-declared variable values on the script, such that errors such as this occur when I try to run the script line by line.

Also, calling x alone to return its value has no effect in atom-script, unlike doing the same in ipython or the python console itself.

Is this the expected behavior?


#12

Why is that such a surprise? The script package has two modes to run code:

  • File Based – runs the entire file
  • Selection Based – runs the selection

The latter runs only the selection, without any knowledge of the code outside the selection. So if the variable declaration is outside the selection, of course you will get an error when using the variable in your selection.


#13

This behavior is very different from the interactive consoles which I am used to.

Even IDEs such as Rstudio keeps track of variables defined on each run (selection based or otherwise) so that we can debug by simply selecting the variable itself to see if it returns the expected value.

Usually, we just have to declare the variable once and it sticks.


#14

I don’t think the script package is an interactive console. The line execution seems to be a straightforward “try running these lines as if they were in a separate file”.

“Even”? Atom’s a text editor; text editors are not (normally) known for having more/better features than IDEs. Of course, there’s nothing stopping Atom from doing what you want. It’s just that this package in particular doesn’t do it.

I don’t know if it’s any better, but you may want to try the hydrogen package. It seems to have some compatability with IPython (when you install the needed kernels; see it’s docs).


#15

Guess I was unwittingly expecting atom-script to behave like an IDE, for lack of experience. Anyway, now that I know how atom-script works, and that it is working normally, I have no further complaints about it. Will give the hydrogen package a try. Thank you.


#16

That’s the important piece of information I was missing. I didn’t know that you were trying to run it line-by-line.


#17

Oh. No wonder you were asking me for a screen-shot. Anyway, I think the issue is settled for now. Thanks for helping!

By the way, seems like atom-script does not currently support line-number based execution of python scripts.

Though I am not intending to raise this as an issue, just thought it would be useful to take note.


#18

On this forum, there is also a moderate incidence rate of people who are trying to execute code that wouldn’t work or that isn’t being matched correctly. I ask for screenshots because I can tell a lot about what’s going on when I can see what someone else sees.