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 to import user defined modules?
-
These are the directories in sys.path (slghtly edited to remove the prefix from root to app):
/Documents/Examples/Animation
/Documents/site-packages
/Pythonista.app/Frameworks/PythonistaKit.framework/pylib
/Pythonista.app/Frameworks/PythonistaKit.framework/pylib_ext
/Pythonista.app/Frameworks/PythonistaKit.framework/pylib/site-packages
/Pythonista.app/Frameworks/PythonistaKit.framework/pylib/site-packages/PIL_compatDocuments itself is not on the path, neither are its subdirectories. Is this a bug? Is it possible to add directories to the path?
-
This isn't a bug, you're supposed to put them in
site-packages
. If you really want, you could putimport sys,os;sys.path.append(os.path.expanduser('~/Documents'))
at the beginning of scripts.EDIT:
Actually, Documents seems to be the first element in sys.path for me. Is this not the case for you? -
The first
sys.path
entry might be the directory of the last script run, and not always~/Documents
itself. -
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.
Thanks!
-
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!
-
@huub Actually, I just realized that it's more permanent if you put my above code in
pythonista_startup
. Create a filepythonista_startup.py
insite-packages
, and put my above code inside. Whatever goes in this file will be run automatically when pythonista starts up -
@Webmaster4o Unfortunately that isn't very permanent either. Any modifications to
sys.path
are 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
pip
work in Pythonista. Bothsetuptools
andpip
are not installed directly into the root ofsite-packages
, so they require the use ofpth
files to be loadable by Python. Thesepth
files are recognized when thesite
module is imported, which happens automatically, but only once when the app is launched. When Pythonista resetssys.path
thesite
module is not fully reloaded, so the additional path entries are lost. -
How can I add the twisted module?
-
@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?
-
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.
Example:
# 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?