Pythonista Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "[Category:Python] == Locating modules in parent directories == Given a project structure like this: <pre> | +- my_module/ | | | +- test/ | | | | | +- test_some_pkg.py | | | |...")
 
No edit summary
Line 1: Line 1:
[Category:Python]
[[Category:Python]]
== Locating modules in parent directories ==
== Locating modules in parent directories ==
Given a project structure like this:
Given a project structure like this:
Line 39: Line 39:


That line should be removed before committing any changes to git. It won't break anything, it's just unnecessary in any other context.
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 and Pythonista ==
Use StaSh, which implements a Bash-like shell in Pythonista. Refer [https://github.com/ywangd/stash to the documentation] for instructions on how to install. <ref>[https://github.com/ywangd/stash StaSh], GitHub</ref>
== Accessing private GitHub repos in StaSh ==
`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. <ref>[https://forum.omz-software.com/topic/2829/git-github-workflow-in-stash/11 Setting up SSH keys], OMZ forum</ref>
From the StaSh prompt:
<syntaxhighlight lang="bash">
[my_project] $ ssh-keygen -trsa -b2048
[my_project] $ pbcopy ~/.ssh/id_rsa.pub
</syntaxhighlight>
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:
<syntaxhighlight lang="bash">
[my_project]$ git remote originssh ssh://git@github.com/jsbain/stash_git_tutorial.git
</syntaxhighlight>
Now you can fetch/push,etc from originssh
<syntaxhighlight lang="bash">
[my_project] git push originssh
</syntaxhighlight>
== Notes ==
<references/>

Revision as of 10:54, 12 January 2018

Locating modules in parent directories

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 and Pythonista

Use StaSh, which implements a Bash-like shell in Pythonista. Refer to the documentation for instructions on how to install. [1]

Accessing private GitHub repos in StaSh

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

  1. StaSh, GitHub
  2. Setting up SSH keys, OMZ forum