Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
How do you run a function?
-
If I create a function def in the editor, how do I run it in the command line window? On a UNIX machine, you can put the function definition in a file in the same current working directory you're running Python in, and then on the Python command line type from func() import *, then call the function with some arguments, and it returns the results. I don't see any way of doing this in Pythonista for iPad. Any help would be appreciated!
-
Put the following in a Pythonista file called "hello_file":
def hello_function(in_first_name = 'Dude'): print('Hello {}!'.format(in_first_name))
On the Pythonista command prompt, type:
from hello_file import hello_function hello_function('Bobk48')
Or:
import hello_file hello_file.hello_function('Bobk48')
It is a Python idiom to avoid using 'from module import *`.
If the above is not working for you, it is probably because you have the double .py problem. If you saved the file as 'hello_file.py' then Pythonista has added an additional '.py' to the end of that. This is not visible to you in the filename in the title bar. However, in the file list flyout at the left of your screen, you should be able to see the file is 'hello_file.py.py'. The only way to fix the double .py issue is to rename the file to remove your .py.
I consider it to be a bug that Pythonista counterintuitively adds .py to a new filename that already ends in .py.
-
ccc, thanks for answering this question! Where you say " put the following in a Pythonista file called 'hello_file' ", how do you do that in IPad Pythonista? I've searched all the button and menu choices in the editor, browser, and command line windows, and there's no file save choice! In the browser, you can create a new empty file for use in the editor window, but I guess my question is "Where is that saved, so to speak?". It seems like editor and command line are disconnected on the IPad. When something is in the editor window, and you press the Run button, it runs that stuff, but where are the files themselves? iPad is not a computer, you have no access to the file system( unless you jailbreak it, I guess.). Any help you could give me on how to save what's in the editor window would be greatly appreciated! Thanks again.
Robert M. Koretsky -
The iPad is a computer and you have access to the file system. You just do not have access to the whole file system. You do however have read/write access to the part of the file system that is owned by the Pythonista app.
Just like other editors, the title of the document that you are editing in Pythonista is in the title bar at the top of the window.
Tapping the document title and then the pencil that appears to the right of the document title lets you save the content to a file with the name of your choice. More info: http://omz-software.com/pythonista/docs/ios/pythonista.html
-
ccc, your explicit instructions worked perfectly, but for a simple function def in a file named test like-
def testr( n ):
print n
On the interactive command line I enter: from test import testr . It likes that, sometimes.
Then: test(4)
And it gives me a name error!Thanks for being so helpful though!
-
My recommendation is that you sit down at your "UNIX" computer and fully debug the whole thing that you are trying to achieve. Once it is working to your satisfaction on "UNIX" then pick up Pythonista and type in exactly the same code. You will find that the two environments work the same way.
In Python, we do not import functions. Instead, we import files (modules, scripts, etc). If those files contain valid functions, then those functions will be imported into a namespace under "<filename>." (with the "py" removed). I made a clear naming distinction between hello_file and hello_function in my example to make this explicit.
From your example above,
from test import testr # from the_file import the_function testr(4) # call the_function
should work.
-
ccc I did exactly what you recommend, put a simple function definition in the editor, named it without the .py extension, imported the file, called the function, and it gives errors! I put the same file into my UNIX current working directory, and imported it, called the function in it, and it works. I also downloaded 2 other Python IDE's to my iPad- PyM and Python2.7. Used the same file and function definitions in them, and both worked perfectly. And we're not talking some 350 line file, it's a 3 line function def that's good Python. There's something wrong with Pythonista for iOS 5, maybe even 6. But I'm not buying a new iPad for $500 to test out Pythonista for iOS 7!
If it doesn't cost me another $7, I will try reloading Pythonista. And experiment again, but I want to test Python functions, not Pythonista. I gave it 1 star out of 5 on the app store. Sorry! -
Ahhh.. Are you running Pythonista on iOS 5 or on iOS 6? If hello_file.hello_function() works fine but your code does not, can you please put your code here so that we can all try it out at home?
-
ccc here it is( on an original iPad with iOS 5)-
In the editor, I create the following as a file named tester:
def test(n):
print n
Then, in the interactive python prompt window, I type-
from tester import test #sometimes works, sometimes doesn't
test(5)#always get a name undefined error here
I've tried rebooting the iPad, deleting the file and using another name, but I don't know if you can restart the interpreter. Using multiple imports from the same file could cause a problem, but I don't know how since I use different file and function names each time!
-
The fact that "sometimes [import] works, sometimes doesn't" is very strange!!
You can use "del" to unload an imported file:
>>> import math >>> math.pi 3.141592653589793 >>> del math >>> math.pi Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'math' is not defined
Does the following work?
import tester tester.test(5)
-
ccc, I'm out of the Pythonista ball game. Best of luck to you! I'm using PyM and Python2.7 on the iPad now, and big Python IDE's like enthought.com's canopy on my Bill, Steve, and Linus machines.