This project is set up for you to practice making and publishing a package to the test package index. To actually push the package out to the internet, you'll need an account with PyPI, which you can get by going here: https://test.pypi.org/account/register/. If you don't care about actually pushing to the package index, you can start at step 1.
Step 0 (only if you want to push your package to the index!):
- Go to the link above and create an account with
test.pypi.org
. - Create a new API token here: https://test.pypi.org/manage/account/#api-tokens
- Set the scope to "Entire account"
- SAVE the token it gives you in a secure location! If you lose it, you'll have to regenerate and replace it - you can't get it back.
- Install some tools first:
py -m pip install --upgrade build
(windows) orpython3 -m pip install --upgrade build
(macox/linux)py -m pip install --upgrade hatchling
(windows) orpython3 -m pip install --upgrade hatchling
(macox/linux)- (if uploading)
py -m pip install --upgrade twine
(windows) orpython3 -m pip install --upgrade twine
(macox/linux)
- Rename
src/package_demo_echarles
tosrc/package_demo_<your_username_here>
. This will prevent naming clashes when you push the package to the index. - In
pyproject.toml
, changename = "package_demo_echarles"
so that the name matches the folder name you made in the last step. You can also change the repo URLs, but that's not as important. - In
example.py
, add a function. It can do anything you want it to do. There are two examples there already. - Run
py -m build
(win) orpython3 -m build
(osx). You should see adist
folder appear! Spend a few minutes and take a look at what build artifacts are inside. If you're feeling extra ambitious, you can unzip both the.tar.gz
and the.whl
files and poke around in the internals. - If you don't want to upload to the package index, you're done! If you do want to, and you have an API key, keep going.
- Run
py -m twine upload --repository testpypi dist/*
(win) orpython3 -m twine upload --repository testpypi dist/*
(osx). - You'll be prompted for a username. Enter
__token__
. - At the next prompt, paste in the token you generated in step 0. You should see something like the following output:
Uploading distributions to https://test.pypi.org/legacy/
Enter your username: __token__
Uploading example_package_YOUR_USERNAME_HERE-0.0.1-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.2/8.2 kB • 00:01 • ?
Uploading example_package_YOUR_USERNAME_HERE-0.0.1.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.8/6.8 kB • 00:00 • ?
- In a browser, open:
https://test.pypi.org/project/example_package_YOUR_USERNAME_HERE
. You should see a page for your new library! - As a final test of your deployment, you can create a new virtual environment (not inside this project folder) and try to install your package:
- in
new_project_dir
, runpython -m venv venv
- Activate the venv:
venv/Scripts/activate.bat
(win) or./venv/bin/activate
(osx) - Install the package from the test index:
py -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-package-YOUR-USERNAME-HERE
(win) orpython3 -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-package-YOUR-USERNAME-HERE
(osx) - Create
new_project_dir/main.py
- add this to
main.py
:from package_demo_YOUR_USERNAME_HERE import example
Now you should be able to call the function you added in step 3:example.your_function_here()
- Profit!
This project draws heavily from the official docs: https://docs.google.com/presentation/d/1Kg6cxi665wukjab6cyF5uubjI0DJSsnjyLbabgFpXyU/edit?usp=sharing