Remembering Python commands becomes much easier (at least for me) once I can relate them to a “big picture” like the one I presented in my last post.
In this post, I will glue some code to each of the three patterns discussed there.
Pattern #1: create a new package or project folder
Please remember, the new package to be developed in this post is called factors; replace it with your own package name.
Make a new directory:
$ mkdir factors $ cd factors
Next, bring your brand new folder under version control:
$ git init
Create and activate a virtual environment:
$ python3 -m venv venv $ source venv/bin/activate
I like to keep my projects in a folder called “projects” and my packages in a folder called “packages” (how amazing is that;). Once I find myself reusing pieces of a specific project over and over again, I move that code from my projects folder into my packages folder after performing some refactoring. Python developers tend to be kind of opinionated when it comes to choosing the optimal file structure for a project or package, but this post provides an excellent overview.
A similar procedure applies when using the package as a dependency in your own projects. To do so, you can perform a so called editable install in the project directory:
$ cd ~/projects $ mkdir niceproject-where-I-want-to-use-my-new-package $ cd niceproject-where-I-want-to-use-my-new-package $ python3 -m venv venv $ source venv/bin/activate $ pip install -e ../../packages/factors
By including the -e flag (or: —editable) in the pip command, you create a symbolic link to your own package (in this example called “factors”). This has the advantage that changes in the package are immediately visible into the project without the need for reinstalling the package.
Pattern #2: interact with other developers using GitHub
Now time has come to define the URL your local repository will get pushed to:
$ git remote add origin remote repository URL
Replace “remote repository URL” with the URL of your remote Github repository. You can find this URL by visiting your your remote repository page on GitHub, clicking the green button “clone or download”.
Before pushing to remote, make sure your working directory is clean:
$ git add . $ git commit -m “A nice description for this commit goes here”
Finally, push your local changes to remote:
$ git push origin master
This pushes your local branch called “master” to your remote repository called “origin”.
Pattern #3: share your package with end users using PyPI
$ cd ~/packages/factors $ source venv/bin/activate $ pip3 install setuptools wheel
Create the distribution archives (a.k.a. the tarball/source archive and the wheel/built distribution):
$ python setup.py sdist bdist_wheel
This should create a new folder named “dist” containing the two archives mentioned above. Now you are ready to upload these two archives to PyPI:
$ twine upload dist/*
By now, users all over the world (how cool it that!) are be able to install your package for use in their own project like so:
$ pip install factors
Feel free to drop me a line about your experience with publishing your own package on PyPI. Happy coding!