Python: Upload your project on PyPI to Anaconda Cloud
Python: Upload your project on PyPI to Anaconda Cloud
Here is the steps to upload a package to conda server. First, a package that will be uploaded to a conda server must be available on Python package index (PyPI). Then you can create a conda recipe from package-PyPI. Then you will build a conda package from conda recipe. After you build a conda package, you can upload it to a channel to make it available for others to use. A channel is a specific location for storing packages, and may point to a cloud-based repository or a private location on a remote or local repository that you or your organization created.
Table of Content
Prerequisites
- conda
conda update conda
- conda-build
conda install conda-build
- anaconda
conda install anaconda-client
- Anaconda account
Go to https://anaconda.org/ and register new account.
- build.sh
"%PYTHON%" setup.py install --single-version-externally-managed --record=record.txt
if errorlevel 1 exit 1
del %SCRIPTS%\conda-init
copy bdist_conda.py %PREFIX%\Lib\distutils\command\
if errorlevel 1 exit 1
- bld.bat
python setup.py install --single-version-externally-managed --record=record.txt
1. Create Conda recipe from PyPI
To create conda recipe from PyPI, type like this:
conda skeleton pypi NAME_OF_PACKAGE
for example, create conda recipe of OctaDist package:
conda skeleton pypi octadist
This will take 1-2 minutes.
When it is done, you should see the message like this:
# output
...
Hunk #1 succeeded at 167 with fuzz 2 (offset 1 line).
Writing recipe for octadist
...
2. Build Conda package
To build conda package, type like this:
conda-build --python PYVER PACKAGE
where
- PYVER is version of Python, for example, 3.7.
- PACKAGE is name of package which must be available on PyPI.
For example:
conda-build --python 3.7 octadist
This will build and test your package. When it is done, a conda package will be created either at $HOME/conda-bld
or at the environment directory:
# output
$HOME/conda-bld/
# or
$HOME/anaconda3/envs/ENV_NAME/conda-bld
3. Convert pure Python packages to other platforms
conda convert --platform PLATFORM FILE -o $HOME/conda-bld/
where
- PLATFORM is name of operating system: osx-64, linux-32, linux-64, win-32, and win-64.
- FILE is Python package (*tar.bz2) which can be found at
$HOME/conda-bld/linux-64/
directory.
For example,
conda convert --platform win-64 $HOME/conda-bld/linux-64/*.tar.bz2 -o $HOME/conda-bld/
This will convert the conda package from linux-64 to win-64 platform, the file will be created at $HOME/conda-bld/win-64
:
$HOME/conda-bld/win-64/*.tar.bz2
4. Login to Anaconda (Conda) server
You have to login to Anaconda server before uploading packages. Use following command:
anaconda login
Then you will be asked username and password:
Using Anaconda API: https://api.anaconda.org
Username:
Password:
5. Upload package to Conda server
Now you are ready to upload all conda packages to the server using anaconda command:
anaconda upload PACKAGE.tar.bz2
For example, upload package for linux-64 platform:
anaconda upload $HOME/conda-bld/linux-64/*.tar.bz2
6. Testing
When it is done without error messages, you can now try installing a package via conda command.
conda install PACKAGE
Optional: Build script for routine job
To create, build, and upload package for all platform automatically, you can use the following bash script to do so:
#!/bin/bash
pkg='octadist'
array=( 3.5 3.6 3.7 )
platforms=( osx-64 linux-32 linux-64 win-32 win-64 )
### 1. create metal.yml
conda skeleton pypi $pkg
### 2. building conda packages
for i in "${array[@]}"
do
conda-build --python $i $pkg
done
### 3. convert package to other platform
CON_BLD=$HOME/conda-bld
# CON_BLD="/home/nutt/anaconda3/envs/py37/conda-bld"
find $CON_BLD/linux-64/ -name *.tar.bz2 | while read file
do
#conda convert --platform all $file -o $HOME/conda-bld/
for platform in "${platforms[@]}"
do
conda convert --platform $platform $file -o $CON_BLD/
done
done
### 4. upload packages to conda
find $CON_BLD/ -name *.tar.bz2 | while read file
do
anaconda upload $file
done
Error and Fixing
- Error about "Unicode string"
String in meta.yml file should be supported by conda-build, if you get error about "Unicode string", you can avoid this error by putting the string to " ... ", for example, the following string has colon (:) which is not supported by conda-build:
Old:
about:
summary: My Pacakge: New package
New:
about:
summary: "My Pacakge: New package"
References and further reading:
- https://docs.python-guide.org/
- https://anaconda.org
- https://medium.com/@giswqs/building-a-conda-package-and-uploading-it-to-anaconda-cloud-6a3abd1c5c52
- https://docs.conda.io/projects/conda-build/en/latest/user-guide/tutorials/build-pkgs-skeleton.html
- https://gist.github.com/rangsimanketkaew/1904bd45c933003ac076823861cd3ca8
Rangsiman Ketkaew