Package: pyproject2conda
pyproject2condaA script to convertpyproject.tomldependencies toenvironemnt.yamlfiles.OverviewThe main goal ofpyproject2condais to provide a means to keep all basic
dependency information, for bothpipbased andcondabased environments, inpyproject.toml. I often use a mix of pip and conda when developing packages,
and in my everyday workflow. Some packages just aren't available on both. If you
use poetry, I'd highly recommendpoetry2conda.FeaturesSimple comment based syntax to add information to dependencies when creatingenvironment.yamlStatusThis package is actively used by the author, but is still very much a work in
progress. Please feel free to create a pull request for wanted features and
suggestions!Quick startUse one of the following to installpyproject2conda:$pipinstallpyproject2condaor$condainstall-cconda-forgepyproject2condaIf using pip, to install withrichandshellinghamsupport, either install
them your self, or use:$pipinstallpyproject2conda[all]The conda-forge distribution oftyper(whichpyproject2condauses) installsrichandshellinghamby default.Example usageBasic usageConsider thetomlfiletest-pyproject.toml.[project]name="hello"requires-python=">=3.8,<3.11"dependencies=["athing",# p2c: -p # a comment"bthing",# p2c: -s "bthing-conda""cthing; python_version < '3.10'",# p2c: -c conda-forge]# ...Note the comment lines# p2c:.... These are special tokens thatpyproject2condawill analyze. The basic options are:usage:-c[-h][-cCHANNEL][-p][-s][packages...]Parsersearchesforcomments'# p2c: [OPTIONS] CONDA-PACKAGESpositionalarguments:packages

options:-h,--helpshowthishelpmessageandexit-cCHANNEL,--channelCHANNELChanneltoaddtothepyprojectrequirement-p,--pipIfspecified,installpyprojectdependencywithpip-s,--skipIfspecifiedskippyprojectdependencyonthislineSo, if we run the following, we get:$pyproject2condayaml-ftests/data/test-pyproject.toml
channels:-conda-forge
dependencies:-bthing-conda-conda-forge::cthing-pip-pip:-athingNote that other comments can be mixed in.By default, the python version is not included in the resulting conda output. To
include the specification frompyproject.toml, use--python-include inferoption:$pyproject2condayaml-ftests/data/test-pyproject.toml--python-includeinfer
channels:-conda-forge
dependencies:-python>=3.8,<3.11-bthing-conda-conda-forge::cthing-pip-pip:-athingAlternate syntax: using table instead of commentsWhile using comments to mark options has the convenience of placing the changes
right next to the dependency, it can becore a bit cumbersome. If you feel this
way, then you can use an alternative method to mappipdependencies tocondadependencies. For this, use thetool.pyproject2.conda.dependenciestable. For
example, we can do the same thing as above with:# ...[tool.pyproject2conda.dependencies]athing={pip=true}bthing={skip=true,packages="bthing-conda"}cthing={channel="conda-forge"}pytest={channel="conda-forge"}matplotlib={skip=true,packages=["additional-thing; python_version < '3.9'","conda-matplotlib"]}build={channel="pip"}# ...Specify python versionTo specify a specific value of python in the output, pass a value with:$pyproject2condayaml-ftests/data/test-pyproject.toml--python-include\python=3.9
channels:-conda-forge
dependencies:-python=3.9-bthing-conda-conda-forge::cthing-pip-pip:-athingNote that this is for including python in the resulting environment file.You can also constrain packages by the python version using the standardpyproject.tomlsyntax"...; python_version < 'some-version-number'". For is
parsed for both the pip packages and conda packages:$pyproject2condayaml-ftests/data/test-pyproject.toml--python-version3.10
channels:-conda-forge
dependencies:-bthing-conda-pip-pip:-athingIt is common to want to specify the python version and include it in the
resulting environment file. You could, for example use:$pyproject2condayaml-ftests/data/test-pyproject.toml--python-version3.10\--python-includepython=3.10
channels:-conda-forge
dependencies:-python=3.10-bthing-conda-pip-pip:-athingBecause this is common, you can also just pass the option-p/--python:$pyproject2condayaml-ftests/data/test-pyproject.toml--python3.10
channels:-conda-forge
dependencies:-python=3.10-bthing-conda-pip-pip:-athingAdding extra conda dependencies and pip requirementsYou can also add additional conda and pip dependencies with the flags-d/--depsand-r/--reqs, respectively. Adding the last example:$pyproject2condayaml-ftests/data/test-pyproject.toml-ddep-rreq
channels:-conda-forge
dependencies:-bthing-conda-conda-forge::cthing-dep-pip-pip:-athing-reqThese will also obey dependencies likedep:python_version<={version}. Pass the
flags multiple times to pass multiple dependencies.Command "aliases"The namepyproject2condacan be a bit long to type. For this reason, the
package also ships with the aliasp2c, which has the exact same functionality.
Additionally, the subcommands can be shortened to a unique match:$p2cy-ftests/data/test-pyproject.toml--python3.10
channels:-conda-forge
dependencies:-python=3.10-bthing-conda-pip-pip:-athingYou can also call withpython -m pyproject2conda.Installing extrasGiven the extra dependency:# ...[project.optional-dependencies]test=["pandas","pytest",# p2c: -c conda-forge]dev-extras=[# p2c: -s "additional-thing; python_version < '3.9'" # additional pkg## p2c: -s "another-thing" # skipped because of ## before p2c."matplotlib",# p2c: -s conda-matplotlib]dev=["hello[test]","hello[dev-extras]"]dist-pypi=[# this is intended to be parsed with --no-base option"setuptools","build",# p2c: -p]# ...and running the following gives:$pyproject2condayaml-ftests/data/test-pyproject.toml-etestchannels:-conda-forge
dependencies:-bthing-conda-conda-forge::cthing-conda-forge::pytest-pandas-pip-pip:-athingpyproject2condaalso works with self referenced dependencies:$pyproject2condayaml-ftests/data/test-pyproject.toml-edev
channels:-conda-forge
dependencies:-additional-thing-bthing-conda-conda-forge::cthing-conda-forge::pytest-conda-matplotlib-pandas-pip-pip:-athingThis also shows thatp2ccomments without dependencies are also parsed. To
comment out such lines, make surep2cis preceded by##.Header in outputBy default,pyproject2condaincludes a header in most output files to note
that the files are auto generated. No header is included by default when writing
to standard output. To override this behavior, pass--header/--noheader:$pyproject2condayaml-ftests/data/test-pyproject.toml--header## This file is autogenerated by pyproject2conda# with the following command:##     $ pyproject2conda yaml -f tests/data/test-pyproject.toml --header## You should not manually edit this file.# Instead edit the corresponding pyproject.toml file.#channels:-conda-forge
dependencies:-bthing-conda-conda-forge::cthing-pip-pip:-athingUsage within pythonpyproject2condacan also be used within python:>>>frompyproject2conda.requirementsimportParseDepends>>>p=ParseDepends.from_path("./tests/data/test-pyproject.toml")# Basic environment>>>print(p.to_conda_yaml(python_include="infer").strip())channels:- conda-forgedependencies:- python>=3.8,<3.11- bthing-conda- conda-forge::cthing- pip- pip:- athing# Environment with extras>>>print(p.to_conda_yaml(extras="test").strip())channels:- conda-forgedependencies:- bthing-conda- conda-forge::cthing- conda-forge::pytest- pandas- pip- pip:- athingConfigurationpyproject2condacan be configured with a[tool.pyproject2conda]section inpyproject.toml. To specify conda channels use:# ...[tool.pyproject2conda]channels=['conda-forge']# these are the same as the default values of `p2c project`template_python="py{py}-{env}"template="{env}"style="yaml"# optionspython=["3.10"]# Note that this is relative to the location of pyproject.tomluser_config="config/userconfig.toml"default_envs=["test","dev","dist-pypi"][tool.pyproject2conda.envs.base]style=["requirements"]# Note that the default value for `extras` is the name of the environment.# To have no extras, either pass# extras = []# or#extras=false## A value of `extras = true` also implies using the environment name# as the extras.[tool.pyproject2conda.envs."test-extras"]extras=["test"]style=["yaml","requirements"][[tool.pyproject2conda.overrides]]envs=['test-extras',"dist-pypi"]base=false[[tool.pyproject2conda.overrides]]envs=["test","test-extras"]python=["3.10","3.11"]Note that specifying channels at the command line overridestool.pyproject2conda.channels.You can also specify environments without the base dependencies (those underproject.dependencies) by passing the--no-baseflag. This is useful for
defining environments for build, etc, that do not require the package be
installed. For example:# ...dist-pypi=[# this is intended to be parsed with --no-base option"setuptools","build",# p2c: -p]# ...These can be accessed using either of the following:$pyproject2condayaml-ftests/data/test-pyproject.toml-edist-pypi--no-base
channels:-conda-forge
dependencies:-setuptools-pip-pip:-buildor>>>frompyproject2conda.requirementsimportParseDepends>>>p=ParseDepends.from_path("./tests/data/test-pyproject.toml")# Basic environment>>>print(p.to_conda_yaml(extras='dist-pypi',include_base=False).strip())channels:- conda-forgedependencies:- setuptools- pip- pip:- buildCreating multiple environments frompyproject.tomlpyproject2condaprovides a means to create all needed environment/requirement
files in one go. We configure the environments using thepyproject.tomlfiles
in the[tool.pyproject2conda]section. For example, example the configuration:# ...[tool.pyproject2conda]channels=['conda-forge']# these are the same as the default values of `p2c project`template_python="py{py}-{env}"template="{env}"style="yaml"# optionspython=["3.10"]# Note that this is relative to the location of pyproject.tomluser_config="config/userconfig.toml"default_envs=["test","dev","dist-pypi"][tool.pyproject2conda.envs.base]style=["requirements"]# Note that the default value for `extras` is the name of the environment.# To have no extras, either pass# extras = []# or#extras=false## A value of `extras = true` also implies using the environment name# as the extras.[tool.pyproject2conda.envs."test-extras"]extras=["test"]style=["yaml","requirements"][[tool.pyproject2conda.overrides]]envs=['test-extras',"dist-pypi"]base=false[[tool.pyproject2conda.overrides]]envs=["test","test-extras"]python=["3.10","3.11"]run through the commandpyproject2conda project(orp2c project):$p2cproject-ftests/data/test-pyproject.toml--dry# --------------------# Creating requirements base.txtathing
bthing
cthing;python_version<"3.10"# --------------------# Creating yaml py310-test-extras.yamlchannels:-conda-forge
dependencies:-python=3.10-conda-forge::pytest-pandas# --------------------# Creating yaml py311-test-extras.yamlchannels:-conda-forge
dependencies:-python=3.11-conda-forge::pytest-pandas# --------------------# Creating requirements test-extras.txtpandas
pytest# --------------------# Creating yaml py310-test.yamlchannels:-conda-forge
dependencies:-python=3.10-bthing-conda-conda-forge::pytest-pandas-pip-pip:-athing# --------------------# Creating yaml py311-test.yamlchannels:-conda-forge
dependencies:-python=3.11-bthing-conda-conda-forge::pytest...Note that here, we have used the--dryoption to just print the output. In
production, you'd omit this flag, and files according to--templateand--template-pythonwould be used.The options under[tool.pyproject2conda]follow the command line options
(replace-with_). To specify an environment, you can either use the[tool.pyproject.envs."environment-name"]method, or, if the environment is the
same as the "extras" name, you can just specify it undertool.pyproject2conda.default_envs:[tool.pyproject2conda]# ...default_envs=["test"]is equivalent to[tool.pyproject2conda.envs.test]extras=["tests"]To specify a conda environment (yaml) file, passstyle = "yaml"(the
default). To specify a requirements file, passstyle = "requirements". You can
specify both to make both.Options in a giventool.pyproject2conda.envs."environemnt-name"section
override those at thetool.pyproject2condalevel. So, for example:# ...[tool.pyproject2conda.envs."test-extras"]extras=["test"]style=["yaml","requirements"]# ...will override use the two styles instead of the default ofyaml.You can also override options for multiple environments using the[[tools.pyproject2conda.overrides]]list. Just specify the override option(s)
and the environments to apply them to. For example, above we specify that the
base option isFalsefor envstest-extrasanddist-pypi, and that the
python version should be3.10and3.11for envstestandtest-extras.So in all, options are picked up, in order, from the environment definition,
then the overrides list, and finally, from the default options.You can also define "user defined" configurations. This can be done through the
option--user-config. This allows you to define your own environments outside
of the (most likely source controlled)pyproject.tomlfile. For example, we
have the optionuser_config=config/userconfig.toml.[tool.pyproject2conda.envs."user-dev"]extras=["dev","dist-pypi"]deps=["extra-dep"]reqs=["extra-req"]name="hello"Note that the full path of this file is note that the path of theuser_conifgfile is relative to thempyproject.tomlfile. So, if thepyproject.tomlfile
is ata/path/pyproject.toml, the path of user configuration files will bea/path/config/userconfig.toml. We then can run the following:$p2cproject-ftests/data/test-pyproject.toml--dry--envsuser-dev# --------------------# Creating yaml py310-user-dev.yamlname:hello
channels:-conda-forge
dependencies:-python=3.10-bthing-conda-conda-forge::pytest-conda-matplotlib-extra-dep-pandas-setuptools-pip-pip:-athing-build-extra-reqCLI optionsSeecommand line interface documentationfor details on the commands and options.Related workThe applicationpyproject2condais used in the development of the following
packages:cmomythermoextraptmmc-lnpymodule-utilitiesanalphipypyproject2condaitself!DocumentationSee thedocumentationfor a look atpyproject2condain action.LicenseThis is free software. SeeLICENSE.ContactThe author can be reached atwpk@nist.gov.CreditsThis package was created usingCookiecutterwith theusnistgov/cookiecutter-nist-pythontemplate.ChangelogChangelog forpyproject2condaUnreleasedSee the fragment files inchangelog.dv0.11.0 — 2023-11-28AddedCan now access "build-system.requires" as an extra. This can be useful for
creating isolated environments to build a package.ChangedCan now specifypipas a conda dependency. This is needed for cases that
there are no pip dependencies in the environment, but you want it there for
installing local packages. This may be the case if usingconda-lockon an
environment. Note that, much like python is always first in the dependency
list, pip is always last.v0.10.0 — 2023-11-17AddedCan now specify conda changes usingtool.pyproject2conda.dependenciestable.
This is an alternative to using# p2c:comments.Refactored code. Splitparsertorequirementsandoverrides. Also
cleaned up the parsing logic to hopefully make future changes simpler.v0.9.0 — 2023-11-14AddedDefault is now to remove whitespace from dependencies. For example, the
dependencymodule > 0.1will becomemodule>0.1. To override this
behaviour, pass the option--no-remove-whitespace.Now supports python version>3.8,<=3.12Can now specifyextras = falsein pyprojec.toml to skip any extras. The
default (extras = true) is the same asextras = [env_name]whereenv_nameis the name of the environment (e.g.,tool.pyproject2conda.envs.env_name).v0.8.0 — 2023-10-02AddedAdded option to either raise error, or print message for environments with no
dependencies.Changedpyproject2conda now works withpyproject.tomlfiles with no dependencies.v0.7.0 — 2023-09-26AddedNow useloggingto print info output.Changedcli now usestyper. Since the program was already typed, this simplifies the
interface.Program can now be called with any ofpyproject2conda,p2c, orpython -m pyproject2conda.Added cli options to web documentation.Fixed small typos and typing issues.The cli option--python-includenow requires an argument. This is due totypernot liking options with zero or one arguments. Instead of the bare
flag--python-includeincluding the python spec frompyproject.toml, you
have to pass--python-include inferto get that behavior.Added extraallto pip install options. The default is to not includerichorshellingham. Usingpip install pyproject2conda[all]includes these
optional packages. Note that the conda-forge recipe is based on the plain
install (i.e., norichorshellingham). However, the conda-froge recipe
fortyperdoes include these. That means, if you want to installpyproject2condawithout the optional extras, you'll have to use pip.v0.6.1 — 2023-09-22ChangedFixed edge case where--overwrite=checkand have auser_config. Now when
usingp2c projectwith auser_configandoverwrite=check, the timestamp
of the output file will be compared to both thefilename=pyproject.tomlanduser_config.v0.6.0 — 2023-09-19AddedAddedprojectsubcommand. This uses a configuration inpyproject.tomlto
build multiple enivonments in one go.Added--depsand--reqsflags to include extra conda and pip requirements.Added--overwriteto check if output file exists.Now (correctly) using rich_click.Added tests for all new cases, and some edge cases.v0.5.1 — 2023-09-09AddedAdded--sort/--no-sortflag to cli. Default is to sort dependencies. This
fixes issues with changing order inpyproject.tomlleading to different yaml
files.ChangedChanged structure of the repo to better support some third party tools.Moved nox environments from.noxto.nox/{project-name}/envs. This fixes
issues with ipykernel giving odd names for locally installed environments.Moved repo specific dot files to theconfigdirectory (e.g.,.noxconfig.tomltoconfig/userconfig.toml). This cleans up the top level
of the repo.added some support for usingnbqato run mypy/pyright on notebooks.Added ability to bootstrap development environment using pipx. This should
simplify initial setup. See Contributing for more info.Main repo now on usnistgov.This software was developed by employees of the National Institute of Standards
and Technology (NIST), an agency of the Federal Government. Pursuant to title 17
United States Code Section 105, works of NIST employees are not subject to
copyright protection in the United States and are considered to be in the public
domain. Permission to freely use, copy, modify, and distribute this software and
its documentation without fee is hereby granted, provided that this notice and
disclaimer of warranty appears in all copies.THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR
IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.Distributions of NIST software should also include copyright and licensing
statements of any third-party software that are legally bundled with the code in
compliance with the conditions of those licenses.