I found the setup.py
file for the pyvmomi module here: “https://github.com/vmware/pyvmomi/blob/master/setup.py“. It looks like a reasonable example of what to do in setup.py.
Pip + git for development
I’m working on what should be a simple raspberry pi display project and I came up with the need for a set of ad-hoc python modules that were installable from my gitlab server. It was a bit of a journey. Here are the broad steps:
- Create a gitlabs project for your python module. Since will probably have a few of these, it might be good to make a group for them right now.
- I think that you can use
git+ssh://git@gitlabs.example.com...
for this but I chose to use a gitlabs impersonation token for this since ssh isn’t installed everywhere and sometimes the installation needs a bunch of hints in~/.ssh/config
. - A standard install will be done with pip as follows pip install
git+https://{user}:{password}@git.example.com/example-group/example-project.git
. If you created a gitlabs impersonation token about, you can substitute it for password here.
Sometimes I need to edit the installed package that I’m working on. The way to do this is to use the –editable flag to pip. To do that you need to specify some extra information to git when checking out the project. I found that this command line:
pip install --editable git+https://{user}:{token}@gitlabs.example.com/example-group/example-project.git#egg={module_name}
I think that the #egg={module_name} piece provides pip with name of the module as installed. I found the documentation that explains this here: “https://pip.pypa.io/en/stable/topics/vcs-support/”. Assuming that you are doing this in a venv, and it doesn’t make sense not to, you’ll get a new directory called venv/src/{module_name}
which has a git checkout of your module so you can edit it to your needs for this particular project.
Python Arg parsing
Everyone should be using the built in python argparser. This is a good example: https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/list_datastore_info.py
Flask + SQLAlchemy instead of FlaskSQLAlchemy
https://towardsdatascience.com/use-flask-and-sqlalchemy-not-flask-sqlalchemy-5a64fafe22a4
Testing python scripts
You have a python script that has some unit tests that you want to run:
python -c "m = __import__("my-script"); m.unittest()"
Runs the unittest() function within your script. N.B. this still works if your script has a hypen in the name which is my style.