1

I test my python code locally by executing the lines after a if __name__== "__main__": clause. I would rather not include these lines in my git repo, but I don't know of a way to exclude them. Any suggestions?

DanGoodrick
  • 2,818
  • 6
  • 28
  • 52
  • 1
    Introduce some `.gitignore`d files that *only* import and invoke the functionality from the files you do include? There are various options for partially excluding files in e.g. https://stackoverflow.com/questions/6557467/can-git-ignore-a-specific-line; they're generally relatively complicated. But including examples of how to use those functions might be helpful to people, why leave them out? – jonrsharpe Nov 18 '19 at 22:22
  • 2
    2nd-ed for "why leave them out?". The whole point of the `if __name__ == '__main__':` idiom is for stuff that _only_ happens when the script is directly executed. If the scripts are intended to be import-only, it causes no issue, and as @jonrsharpe mentioned, could help someone else down the road. – b_c Nov 18 '19 at 22:25
  • 5
    An even better solution would be to move the testing code into actual test files, so that they can be automatically executed to check the code works, and include them in the repo – jonrsharpe Nov 18 '19 at 22:28
  • 2
    Tests are code. You should keep the tests under version control, like any other code. If that's because you are manually editing and running your test tries, follow jonrsharpe advice. – progmatico Nov 18 '19 at 23:13

0 Answers0