NoonanRosenblum
NoonanRosenblum

Reputation: 533

How quickly set up a python environment to work and tweak a GitHub downloaded repo?

Can somebody simply explain to me what is the correct workflow to download a GitHub code and work on it, quickly try tweaks and fixes ?

Solution 1

  1. install the GitHub repo using pip
  2. then python "functions" are called using shell commands, not in Python shell, but in the macOS shell.

This is clearly not the good solution as I don't even know where is the source code. And not use the macOS shell. I want to see all the files, copy/paste, correct and try things.

Solution 2

  1. Download the GitHub repo in a working directory.
  2. Open a macOS shell.
  3. cd in the directory.
  4. Update PYTHONPATH to the current directory.
  5. Start the python shell.
  6. Load the desired script to try using the import directive.
  7. Edit the script using a text editor and repeat action 6 to try things.

It feels weird to update PYTHONPATH each time I want to work on it. But the weirder is that because the initial script is done to be used in the macOS shell, I cannot pass argument to the main() function when calling it through the Python shell.

Am I missing something here ? Is it normal that python repos look like released code that can hardly be tweaked and quickly tested ?

Upvotes: 0

Views: 78

Answers (1)

zmo
zmo

Reputation: 24812

None of those solutions are the right way to do it. When you need to patch a python code, because you want to add a feature or fix a bug, you should proceed as follows:

Solution 3

  1. Find the project's main page (on github, gitlab or anywhere else), usually a lookup DDG or google or --help will point you the right direction.
  2. Read the project's README file, and look for a developers section
  3. Checkout the project issue tracker for feature requests and bug reports to be sure to check what you'll do isn't being worked on by someone else (and then you can offer your help), that's important for collaboration
  4. For building and developing, follow instructions from the README, which usually look like the following, or just follow this:

How to work on a python project

  1. copy the repository URL (https://github.com/<user>/<project>)
  2. open a terminal in a directory where you want to keep your on-going work (I like to use ~/Workspace)
  3. run git clone https://github.com/<user>/<project>
  4. cd <project>
  5. virtualenv var (you can use whatever name you want, but my preference goes for var)
  6. var/bin/pip install -r requirements.txt
  7. run var/bin/python and all the modules you're developing are available in the python REPL.

This should apply to the vast majority of python projects. Some projects will show you other tools like virtualenv for that step (hint: if there's a requirements.txt you know it's what you'll do), or will use zc.buildout or will use pipenv, but those will usually tell how to build in the README.

Your questions

It feels weird to update PYTHONPATH each time I want to work on it. But the weirder is that because the initial script is done to be used in the macOS shell, I cannot pass argument to the main() function when calling it through the Python shell.

you're right to feel weird doing that. Tools like virtualenv or buildout are there to create contained environments for you to develop comfortably. You'll even find the script you usually find in your path available in the env's bin directory. You'll also find the test suite so that you can test your changes against regressions!

Is it normal that python repos look like released code that can hardly be tweaked and quickly tested ?

usually development happens in a specific branch on a project. So on the github page, you might see the stable/release branch, whereas all the new stuff happens in the devel branch. always look at the issues, and first communicate with the project's maintainer before doing code, at risk of having your contribution rejected.

Upvotes: 2

Related Questions