Refer to the tutorial in the book It's Django I wrote
I will directly excerpt the relevant paragraph for you to see:
Upload your own kit
We have seen and used so many packages written by others. Do you really want to write one yourself? The previous content of this chapter is enough for everyone to understand how to write a package and use it locally, but how do we share this package with the world? Of course, you need to upload the package to PyPI! (This way everyone can use pip to download and install your package). This section is to teach you how to do it.
If you plan to upload your own package, you must first register on PyPI to obtain an account and password.
First of all, everyone needs to know that if your Python project not only wants to be able to import from the current or relative folder, but also wants to be able to be installed into the Python environment, you must write a setup.py file under the project directory. Here we need to upload to PyPI to customize our setup.py. Let’s first check the structure of a project to be uploaded and the locations of several configuration files we want to add:
We noticed that there were originally two packages under this projectpkgA、pkgB(通常我們一個專案裡面會有數個套件,而我們在上傳專案的同時要將所屬的套件全數上傳),裡面分別有數個模組跟標示其為套件結構的__init__.py。接著我們會有一個說明檔README(用來介紹該專案的結構與使用方式,在此便不細談)和一個執行檔(整個專案的執行腳本碼)runner.
Then we need to add two configuration files to the original project, namely setup.py和.pypirc and the .pypirc file. We will introduce how to write these files one by one.
Let’s see it firstsetup.py:
from distutils.core import setup
setup(
name = 'MyProject',
packages = ['pkgA', 'pkgB'],
scripts = ['runner'],
version = '1.0',
description = 'My first project',
author = 'dokelung',
author_email = 'dokelung@gmail.com',
keywords = ['Good Project'],
classifiers = [],
)
We must start with the distutils.core(distutils是Python內建的套件)中匯入setup function. This function will help us install it. Let us understand the meaning of each parameter in this function:
Field name
Description
name
Project name (same name as the project directory)
packages
Name of package to install
scripts
script name, usually represents an executable file, not necessarily
version
Version
description
Project description
author
Author
author_email
Author’s mailbox
keywords
Some keywords for this project
Here is a little explanationscripts,這邊要寫在scripts裡的是整個專案的執行檔,他可能用到了專案裡面的套件。為了要將該檔案同時也裝到使用者的系統上,我們需要把他也標註上去,否則後面我們利用pip安裝的時候就只會安裝package而不會安裝執行檔了。而這邊所謂對執行檔的安裝,其實也就是把指定的scripts放到一個可執行路徑裡,例如/usr/bin/, so that the user can run the script anywhere after installation (in fact, we can specify the installation path, but if we only give the script name, it will be placed in the default location) . One thing you must pay attention to here is that the name of the script must not be the same as the pakcage it is to import. This will lead to some import errors.
At this point, you already have a beautiful installation file (it also supports PyPI release).
Next, let’s take a look at the .pypirc file. Only by creating this profile can we transfer things to PyPI:
If you are a Windows user, please open the terminal (command prompt) and set the environment variables:
set HOME=C:\Users\Owner\
Then make a copy of our user folder in slot C (actually C:Users)裡面新增一個子目錄Owner,把我們的.pipyrccopy it and put it in this folder, the setting is complete.
If you are a Linux or Mac user, please also copy .pypirc to the home directory:
$ cp .pypirc ~/.pypirc
After the above configuration file is ready, it is time to reach the last stage, first register:
$ python setup.py register -r pypi
Then upload:
$ python setup.py sdist upload -r pypi
Great, you have successfully made your work visible to the whole world. Go to PyPI and take a look at your package page. Finally, we tasted the sweet fruit, and then used pip to download our project (and the packages inside) and install it on our own computer to have a look:
$ pip install MyProject
If readers use pip to install the uploaded project on their computer, the package under the project can be imported anywhere on the local machine.
Then you can tell the world that you are also a contributor to Python.
Refer to the tutorial in the book It's Django I wrote
I will directly excerpt the relevant paragraph for you to see:
Upload your own kit
We have seen and used so many packages written by others. Do you really want to write one yourself? The previous content of this chapter is enough for everyone to understand how to write a package and use it locally, but how do we share this package with the world? Of course, you need to upload the package to PyPI! (This way everyone can use pip to download and install your package). This section is to teach you how to do it.
First of all, everyone needs to know that if your Python project not only wants to be able to import from the current or relative folder, but also wants to be able to be installed into the Python environment, you must write a
setup.py
file under the project directory. Here we need to upload to PyPI to customize our setup.py. Let’s first check the structure of a project to be uploaded and the locations of several configuration files we want to add:We noticed that there were originally two packages under this project
pkgA
、pkgB
(通常我們一個專案裡面會有數個套件,而我們在上傳專案的同時要將所屬的套件全數上傳),裡面分別有數個模組跟標示其為套件結構的__init__.py
。接著我們會有一個說明檔README
(用來介紹該專案的結構與使用方式,在此便不細談)和一個執行檔(整個專案的執行腳本碼)runner
.Then we need to add two configuration files to the original project, namely
setup.py
和.pypirc
and the.pypirc
file. We will introduce how to write these files one by one.Let’s see it first
setup.py
:We must start with the
distutils.core
(distutils是Python內建的套件)中匯入setup
function. This function will help us install it. Let us understand the meaning of each parameter in this function:Here is a little explanation
scripts
,這邊要寫在scripts裡的是整個專案的執行檔,他可能用到了專案裡面的套件。為了要將該檔案同時也裝到使用者的系統上,我們需要把他也標註上去,否則後面我們利用pip安裝的時候就只會安裝package而不會安裝執行檔了。而這邊所謂對執行檔的安裝,其實也就是把指定的scripts放到一個可執行路徑裡,例如/usr/bin/
, so that the user can run the script anywhere after installation (in fact, we can specify the installation path, but if we only give the script name, it will be placed in the default location) . One thing you must pay attention to here is that the name of the script must not be the same as the pakcage it is to import. This will lead to some import errors.At this point, you already have a beautiful installation file (it also supports PyPI release).
Next, let’s take a look at the
.pypirc
file. Only by creating this profile can we transfer things to PyPI:If you are a Windows user, please open the terminal (command prompt) and set the environment variables:
Then make a copy of our user folder in slot C (actually
C:Users
)裡面新增一個子目錄Owner
,把我們的.pipyrc
copy it and put it in this folder, the setting is complete.If you are a Linux or Mac user, please also copy
.pypirc
to the home directory:After the above configuration file is ready, it is time to reach the last stage, first register:
Then upload:
Great, you have successfully made your work visible to the whole world. Go to PyPI and take a look at your package page. Finally, we tasted the sweet fruit, and then used
pip
to download our project (and the packages inside) and install it on our own computer to have a look:Then you can tell the world that you are also a contributor to Python.
Questions I answered: Python-QA