How to Upload My Package to Conda

https://github.com/conda/conda

Conda is a python packet manager similar to pip. If you are working on building a python library then it's highly probable yous will exist publishing information technology to Conda as well. Otherwise, Conda users won't benefit from your library which is a negative point for your users and you likewise. Conda documentation has step by steps on how to publish a python packet to Conda from scratch. Only if you have no prior feel with Conda publishing and then you will face a lot of troubles and the official documentation will not be much helpful for you lot. Here I am going to explicate each and every pace in particular which volition help to clarify all of your bug.

What volition you need?

You need to take these tools installed on your build environs.

  1. Miniconda
  2. conda-build
  3. anaconda-customer

Let's showtime!

Allow's assume I'one thousand going to publish the library named my-py-lib to Conda.

The most important task is preparing the meta.yamlfile for your python library. This is the only required file for Conda publishing except for setup.py file. Yep, the documentation says build.sh and build.bat are also required, but trust me it's non! You lot can utilize the aforementioned setup.py file you lot used for pip, to publish to Conda equally well. You lot can place the meta.yaml file anywhere you desire. But it's always a practiced thought to create a new directory called conda(or with whatever other name you prefer) in the root directory of your project and place meta.yaml in it. Here is my project structure.

          .
|-conda
| |-meta.yaml
|-setup.py

Hither is the bare minimum meta.yaml file required to publish a Python package to Conda.

meta.yaml

          package:
name: "my-py-lib"
version: "0.1.0"
about:
summary: "This is an awesome Python library"
source:
path: ..
build:
script: python setup.py install

Important points:

  • source.path - should exist the relative path from your meta.yaml to setup.py file.
  • build.script - is the command to build your python distribution. This can be varied depending on your project.

Now yous are ready to publish your package to conda. Execute this command to build the distribution from the projection root directory.

          conda build --output-folder ./conda-out/ ./conda/        

Input statement for conda build should be the directory where your meta.yaml file is located.

If you accept enabled anaconda upload in your build environs this volition upload the package to anaconda subsequently successfully building it. If y'all take disabled anaconda upload so yous may have to run the following command to upload the distribution to anaconda (Update the path of your tar.bz2 parcel as necessary. If y'all haven't login to anaconda from your CLI before so you have to gear up it as described here first.

          anaconda upload ./conda-out/osx-64/my-py-lib-0.1.0-0.tar.bz2        

Congratulations! Your bundle should exist now bachelor in Anaconda. Let'southward dive into some advance configuration options you might need when publishing your package to conda.

Build for multiple python versions

In that location might be special requirements with your library where you require the same python version at runtime which y'all used at the build time. Let'south presume yous are using python three.viii and you need to install a variant of your library which was congenital using the same python version which is 3.8. This can exist achieved with version pinning. Theconda_build_config.yaml file will permit you lot practice this easily.

conda_build_config.yaml

Create a file named as conda_build_config.yaml in the same directory where your meta.yaml file is located. Creating a separate directory equally I said before (in my instance condadirectory) for conda specific files volition be handy when you lot take more files besides meta.yaml. Here is my conda_build_config.yaml

          python_version:
- 3.7
- 3.8
- three.ix

I desire my library to exist built for python 3.7, 3.viii and 3.9 versions separately. Then you have to refer these python versions in your meta.yaml. Here is my updated meta.yaml.

          package:
proper noun: "my-py-lib"
version: "0.1.0"
about:
summary: "This is an awesome Python library"

requirements:
build:
- python={{ python_version }}

run:
- python={{ python_version }}
source:
path: ..
build:
script: python setup.py install
cord: py{{ python_version | replace(".", "") }}

If you want to pin the versions of other dependencies too, then you can include them also in the meta.yaml and conda_build_config.yaml. Now if y'all run the conda build control yous will see multiple build variants in your output directory. If you want you lot tin can modify the build variants' naming design by changing the build.string in meta.yaml.

Build for multiple platforms

You might have noticed that when you execute the conda build in the previous section, your build artefact should have been created in a sub-directory with the platform name of your build surround (i.e osx-64 , linux-64). That means after you pushing this build to the anaconda, this tin exist downloaded simply by the same platform. Therefore if you want to support multiple platforms then you have to convert this build artefact for other platforms and upload to anaconda again. You can easily do that with the following command. I am converting the artefact built for osx-64 to linux-64here.

          conda convert --platform linux-64 ./conda-out/osx-64/my-py-lib-0.1.0-0.tar.bz2  -o ./conda-out        

Then you accept to upload this once again.

          anaconda upload ./conda-out/linux-64/my-py-lib-0.1.0-0.tar.bz2        

You can find all supported platforms here. But you have to do this if and merely if your library is platform dependent. If information technology's non yous can build that as an architecture independent package by updating the buildsection of your meta.yaml as follows. Wait into docs for more than options.

                      build:
noarch: python

Employ variables in meta.yaml

There may exist many situations where y'all need to define variables and read environment variables when defining your meta.yaml. Thanks to Jinja2 support in meta.yaml it'southward possible.

Employ environs variables

          package:
proper noun: "my-py-lib"
version: "{{ VERSION }}"

conda build volition look for the VERSION environment variable in the build environment every bit the version value.

Read setup.py arguments.

          {% set data = load_setup_py_data() %}

packet:
proper name: "my-py-lib"
version: {{ information.get('version') }}

conda build will extract the value of version field of setup.pyequally the version value.

Support circuitous build scripts

If you take a complex build script you can remove build.script department from meta.yamland create a carve up build.sh file.

build.sh

Create a build.sh file in the same directory where your meta.yaml file is located. Write your build steps in this file. build.sh is just for Unix similar build environments. If you are building on a Windows environment then you take to create a build.bat file complying with Windows shell standards. Wait into the Conda documentation for more info.

          #!/usr/bin/env bash              
$PYTHON setup.py install

Extra Notes

  • For full syntax reference of meta.yaml- Defining metadata
  • More than details on conda_build_config.yaml- Build variants
  • If you are using Github actions at that place are many prebuilt deportment.

hastingsfuser2001.blogspot.com

Source: https://medium.com/analytics-vidhya/publish-a-python-package-to-conda-b352eb0bcb2e

0 Response to "How to Upload My Package to Conda"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel