[Tutor] Two Scripts, Same Commands, One Works, One Doesn't

Steven D'Aprano steve at pearwood.info
Wed May 15 20:12:02 EDT 2019


Hi Stephen,

My responses are interleaved with your comments below. Please excuse the 
length of my post, but your problem is a complicated one.


On Wed, May 15, 2019 at 08:16:02AM -0400, Stephen P. Molnar wrote:
> I am writing scripts to semi-automate some of my Quantum Chemistry 
> software and have encountered a problem that has me baffled. The two 
> scripts have the same form, the only difference being the commands. One 
> script works, the other bombs.

A note on terminology: to programmers, the terminology "bombs" usually 
refers to a fatal application crash detected by the operating system:

https://en.wikipedia.org/wiki/Bomb_%28icon%29

whereas what you are experiencing is merely an exception with a 
traceback displayed. The difference is that a "bomb" (or a "crash") is a 
serious bug in the application (Python), while an exception is actually 
a sign that Python is working correctly.

The bottom line is that there's not much you can do about a bomb except 
stop running the script, whereas an exception means there's a bug in 
your code you can fix.


> The script that works is:
[...]

Thanks for the careful and detailed samples of your code, but in general 
unless you are paying a consultant to to the work for you, it is best to 
try to narrow down the samples to the smallest amount that demonstrates 
the problem. We're volunteers, donating our time for free, and often 
with very limited time to offer. Every extra line of code you present us 
with discourages us a little more from tackling your problem, so the 
smaller the amount of code, the more likely we are to help.

For a better explanation, you might like to read this:

http://www.sscce.org/

It is written for Java programmers but applies to any language.



> #!/usr/bin/env python

This line tells a Unix system to run the script using "python", which is 
probably going to be Python 2. But I see from your code below that you 
are actually running Python 3 code. So this is a potential problem. I 
cannot tell if it is *actually* a problem right now, or will only become 
one in the future.

You could try replacing the word "python" with "python3", but you should 
run these commands at the Unix shell first:

    env python
    env python3

and see what they say.

Another problem is that both of your scripts call out to an external 
script `pythonsh` which we cannot see, so there's no way of knowing what 
that is doing.

The second script refers to yet another Python script 
`prepare_pdf4.py` but the output says this file doesn't exist:

can't open file
'./prepare_pdf4.py': [Errno 2] No such file or directory


So that's a problem you need to fix. I have no idea if that will fix 
everything, or just part of the issue.



> while the script that bombs is:

[...]
> Traceback (most recent call last):
> 
>   File "<ipython-input-1-078e132fa938>", line 1, in <module>
> runfile('/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py', 
> wdir='/home/comp/Apps/Models/1-NerveAgents/Ligands')
> 
>   File 
> "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", 
> line 824, in runfile
>     execfile(filename, namespace)


This tells me that you are running your code via the Spyder IDE. That 
means you have *at least* five and maybe six components involved:


- the Python interpreter
- which runs the Spyder IDE
- which runs your script
- which runs the mystery file pythonsh;
- which does something (runs it?) with the prepare_ligands4.py 
  file or the (missing) prepare_pdf4.py file;
- which does who knows what.

When having problems debugging this code, it helps to simplify the 
problem by cutting Spyder out of the chain. If you run your script 
directly, using a command like

    python3 path/to/the/script.py

does the error go away? I suspect not, but you can keep that in mind for 
future debugging.



>   File 
> "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", 
> line 110, in execfile
>     exec(compile(f.read(), filename, 'exec'), namespace)
> 
>   File "/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py", line 
> 23, in <module>
>     print ('Subprocess FAILED:', proc.command)
> 
> AttributeError: 'Popen' object has no attribute 'command'

That error message is completely correct: Popen objects don't have an 
attribute called "command".

You are tring to print your own error message, referring to 
"proc.command" but there is no such thing. What are you trying to do? 
You need to find another way to do it.


-- 
Steven


More information about the Tutor mailing list