Xojo Launches Python Script - Won't Save Resulting .png File

I have Xojo launching a Python script

Dim sh As New Shell
sh.Execute("python", textfield1.text)
TextArea1.Text = sh.Result

The script is intended to gather some data, display a graph in png format and then save the graph as a file. This works fine when running the python script from the Terminal as well as running it from Idle. Unfortunately, it only works partially when launched from Xojo. It successfully gathers the data and displays the graph, but it doesn’t save the file and throws a Permission denied error. Any suggestions as to why it works in Terminal and Idle but not when launched from Xojo?

Traceback (most recent call last): File "/Users/chris/Documents/football_graph.py", line 29, in <module> plt.savefig('sample_m.png') File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 561, in savefig return fig.savefig(*args, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/figure.py", line 1421, in savefig self.canvas.print_figure(*args, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/backend_bases.py", line 2220, in print_figure **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/backend_bases.py", line 1962, in print_png return agg.print_png(*args, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/backends/backend_agg.py", line 510, in print_png filename_or_obj = open(filename_or_obj, 'wb') IOError: [Errno 13] Permission denied: 'sample_m.png'

Can you post or upload your script? Where do you want to save your file?

The python code is:

import matplotlib.pyplot as plt plt.scatter(y, x) plt.savefig('sample_m.png') plt.show() plt.close()

try expressing a FULL PATH in savefig instead of just a filename… otherwise it will attempt to save it one place in DEBUG mode and somewhere else in COMPILE mode

Thank you Dave S & Beatrix Willius.

I changed: plt.savefig(‘sample_m.png’)

to a full path: plt.savefig(’/Users/myUserName/Documents/sample_m.png’)

and it is working now. :-). Much appreciated.

try this plt.savefig("~/Documents/sample_m.png’)

that should insure it works for users other than yourself

I did try that but got the following:

FileNotFoundError: [Errno 2] No such file or directory: ‘~/Documents/sample_m.png’

Guess you are on Windows then

No a Mac.

Then I’m not sure why that syntax doesn’t work, unless Python doesn’t allow it… as it is correct, I just tested it in Terminal

ls ~/documents

Xojo’s shell is not running bash, zsh, tcsh etc. like you get in terminal and those command interpreters are what expand the ~ to mean “your home directory”.

See Tilde Expansion (Bash Reference Manual)
and note this is part of BASH - not raw shells.

From http://documentation.xojo.com/index.php/Shell
The Shell is not equivalent to the Terminal or Command app for your OS. Paths and other default settings will likely not be the same. If you need to do configuration of the Shell before you use it, be sure to set it up to be interactive so you can set up the configuration before calling other shell commands.

I beg to differ… Xojo IS running BASH, or at least the is what it reports is running

		Dim sh As New shell
		sh.Mode = 0
		sh.Execute"echo $0"
		msgbox sh.Result

and the Shell IS expanding it…

sh.Execute"ls ~/documents"

returns the list of files in SH.RESULTS

That probably could have been worded better. It doesn’t start bash up the same way that Terminal does. Terminal runs all your login scripts & several others to initialize bash - Xojo shell doesn’t.

Hence why we tell people:
The Shell is not equivalent to the Terminal or Command app for your OS. Paths and other default settings will likely not be the same. If you need to do configuration of the Shell before you use it, be sure to set it up to be interactive so you can set up the configuration before calling other shell commands.

either way Shell DOES expand “~”… perhaps it is PYTHON that doesn’t