Pythonista Cookbook
Locating modules in parent directories[edit]
Given a project structure like this:
| +- my_module/ | | | +- test/ | | | | | +- test_some_pkg.py | | | | | +- [etc...] | | | +- some_pkg.py | | | +- [etc...} | +- some_top_level_file.py | +- [etc...]
In my_module/test/test_some_pkg.py some_pkg is imported with:
from my_module import some_pkg
If you have my_module/test/test_pkg_a.py open in Pythonista and try to run it, you'll get an error that it doesn't know anything about my_module.
(By contrast, in PyCharm, you can specify the base path of the project in the Run settings which will avoid this.)
In order to get around this in Pythonista, add the top-level path to PYTHONPATH by adding this before importing some_pkg:
import sys; sys.path.append('../../')
That line should be removed before committing any changes to git. It won't break anything, it's just unnecessary in any other context.
Git, pip and Pythonista[edit]
Use StaSh, which implements a Bash-like shell in Pythonista. Refer to the documentation for instructions on how to install. [1]
The git and pip commands are both available from the StaSh shell.
It appears that currently Stash doesn't support pip installing from a file, e.g. requirements.txt.
Accessing private GitHub repos in StaSh[edit]
git push can be problematic when run within StaSh. It doesn't prompt for a user name and password like bash does.
The solution is to create a SSH key for StaSh on GitHub. [2]
From the StaSh prompt:
[my_project] $ ssh-keygen -trsa -b2048 [my_project] $ pbcopy ~/.ssh/id_rsa.pub
creates a key, and copies the public key to the clipboard.
You can then paste this into whatever you use for setting up the keys on the server. E.g. go to GitHub and use this key to create a new SSH key there.
Next, we need to add a remote to an existing repo:
[my_project]$ git remote originssh ssh://git@github.com/jsbain/stash_git_tutorial.git
Now you can fetch/push,etc from originssh
[my_project] git push originssh
Notes[edit]
- ↑ StaSh, GitHub
- ↑ Setting up SSH keys, OMZ forum