How to import user defined modules?
Although not at all new to programming, I am new to Python(ista). Let me state upfront that I am really impressed. There are only a few full fledged programming environments for iOS and Pythonista is one of the best. However, I have a few questions.
If I understand correctly there is only little difference between scripts and modules. Both are files with a .py extension. Scipts contain definitions and executable statements (but there are exceptions) and modules contain only definitions (but there are exceptions).
I used the script editor to create a .py file and tried to import it as a module in the interactive prompt. However I get an error message that no module with that name can be found.
How can I define a module and import it in a script or in the interactive prompt?
Thanks for any help!
Olaf last edited by
importsearches for the module in all directories mentioned in
sys.path. Does that include the directory where you put your module? You need to
import systo inspect the
These are the directories in sys.path (slghtly edited to remove the prefix from root to app):
Documents itself is not on the path, neither are its subdirectories. Is this a bug? Is it possible to add directories to the path?
Webmaster4o last edited by Webmaster4o
This isn't a bug, you're supposed to put them in
site-packages. If you really want, you could put
import sys,os;sys.path.append(os.path.expanduser('~/Documents'))at the beginning of scripts.
Actually, Documents seems to be the first element in sys.path for me. Is this not the case for you?
dgelessus last edited by
sys.pathentry might be the directory of the last script run, and not always
No, Documents is not on the path at all. Strange that we have different paths... I was aware of site-packages but it is much easier to develop in anorher directory and import directly from there. When the module is complete it can be moved to site-packages.
Thanks for the tip about os.path.expanduser! I considered adding a directory to sys.path but typing an absolute path is nearly impossible. Expanduser makes it possible.
Dgelessus was right, the directory from which the last script was run is the first entry in sys.path. This inspired the following workaround: put an empty script in the directory from which you want to import. Run it, and the directory is on the path. You can import modules from that directory!
Webmaster4o last edited by
@huub Actually, I just realized that it's more permanent if you put my above code in
pythonista_startup. Create a file
site-packages, and put my above code inside. Whatever goes in this file will be run automatically when pythonista starts up
dgelessus last edited by
@Webmaster4o Unfortunately that isn't very permanent either. Any modifications to
sys.pathare lost the next time you run a script, presumably because Pythonista adjusts the first path entry.
I ran into this issue when trying to make
pipwork in Pythonista. Both
pipare not installed directly into the root of
site-packages, so they require the use of
pthfiles to be loadable by Python. These
pthfiles are recognized when the
sitemodule is imported, which happens automatically, but only once when the app is launched. When Pythonista resets
sitemodule is not fully reloaded, so the additional path entries are lost.
tmillermillert last edited by
How can I add the twisted module?
ccc last edited by
@tmillermillert Please search the forum for the word twisted and then if you do not have what you need, please reask your question on one of those that has more twisted related content. (Twisted is supercool)
Thank you very much for this discussion, I understand a lot more abuot the mechanisms behind imports and sys.path and I believe that I can solve or at least work around most issues. However, I think it is a good idea to provide a bit more control about the contents of sys.path.
Would it be possible to add a setting where you can check for each directory in your user space if it has to be included in sys.path?
AtomBombed last edited by
All you have to do is put the module file inside of the site-packages folder that comes with Pythonista when you download it. (If you have deleted it, just make a new one with that exact folder name), and then when you make an "import" statement, just type in the name of the .py file for the module you just put in the site-packages folder without the .py.
# I make a module here, with a filename called 'mymodule.py' def testfunction(): return True
If I want to have another file use this module, just do this, in a new file:
import mymodule # notice how there isn't a .py print mymodule.testfunction()
When I run that file, it will print "True" in the output console. Does any of that help?