macro128 / pdm-conda

A PDM plugin to resolve/install/uninstall project dependencies with Conda
33 stars 1 forks source link

pdm-plugin is not initialized error on `pdm add --conda` #40

Open suhail-singh opened 2 months ago

suhail-singh commented 2 months ago

Environment details

PDM, version 2.15.4
Installed packages:
pdm-conda               0.18.1     A PDM plugin to resolve/install/uninstall project dependencies with Conda

Minimum reproducible error

Create a new environment using the following commands, selecting python = 3.12 and default values for the other prompts.

cd /tmp
rm -rf /tmp/foo
mkdir -p foo && cd foo
pdm self add pdm-conda
pdm init --runner mamba # using python 3.12 and defaults

Having done so, adding a conda dependency via the default channel using below:

pdm add --conda "postgresql>12.0"

Results in the following error:

Adding packages to default dependencies: postgresql>12.0
STATUS: Resolving dependencies
See /home/user/.local/state/pdm/log/pdm-lock-pl1kd4bb.log for detailed debug log.
[CondaResolutionError]: Conda requirement postgresql>12.0 detected but pdm-plugin is not initialized.
WARNING: Add '-v' to see the detailed traceback

Notes

Since I haven't yet been able to get pdm-conda to work, it's possible that I am simply using it incorrectly. If so, could you please point me to the relevant documentation that provides relevant guidance?

macro128 commented 2 months ago

Hi! Could you provide your pyproject.toml and pdm add --conda "postgresql>12.0" -vv output? PS: If you read the README from this repo then you read all the available docs

suhail-singh commented 2 months ago

Sure. Just to be clear, the sequence of commands that I have run are:

cd /tmp
rm -rf /tmp/foo
mkdir -p foo && cd foo
pdm self add pdm-conda
pdm init --runner mamba # using python 3.12 and defaults
pdm add --conda "postgresql>12.0" -vv

Could you please confirm if you are able to reproduce the failure with the above set of commands run in order?

and pdm add --conda "postgresql>12.0" -vv output

The output of the last command is:

Adding packages to default dependencies: postgresql>12.0
STATUS: Resolving dependencies
Traceback (most recent call last):
  File "/path/to/anaconda3/bin/pdm", line 10, in <module>
    sys.exit(main())
             ^^^^^^
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm/core.py", line 359, in main
    return core.main(args or sys.argv[1:])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm/core.py", line 277, in main
    raise cast(Exception, err).with_traceback(traceback) from None
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm/core.py", line 272, in main
    self.handle(project, options)
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm/core.py", line 208, in handle
    command.handle(project, options)
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm_conda/models/config.py", line 315, in decorator
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm_conda/cli/commands/add.py", line 125, in handle
    super().handle(project, options)
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm/cli/commands/add.py", line 69, in handle
    self.do_add(
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm/cli/commands/add.py", line 162, in do_add
    resolved = do_lock(
               ^^^^^^^^
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm/cli/actions.py", line 101, in do_lock
    mapping, dependencies = resolve(
                            ^^^^^^^^
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm/resolver/core.py", line 39, in resolve
    result = resolver.resolve(requirements, max_rounds)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/anaconda3/lib/python3.11/site-packages/pdm_conda/resolvers.py", line 230, in resolve
    raise CondaResolutionError(f"Conda requirement {r} detected but pdm-plugin is not initialized.")
pdm_conda.conda.CondaResolutionError: Conda requirement postgresql>12.0 detected but pdm-plugin is not initialized.

In the output above, I have edited the path to anaconda directory and replaced that with the generic value of /path/to/anaconda3.

Could you provide your pyproject.toml

The pyproject.toml is the default skeleton generated with the pdm init command. Its contents are:

[project]
name = "foo"
version = "0.1.0"
description = "Default template for PDM package"
authors = [
    {name = "$user", email = "$email"},
]
dependencies = []
requires-python = "==3.12.*"
readme = "README.md"
license = {text = "MIT"}

[tool.pdm]
distribution = false

[tool.pdm.conda]
runner = "mamba"
channels = []

If there is some minimal configuration that needs to be added over and above what's generated by pdm init, could you please share what that is?

macro128 commented 2 months ago

Hi! I can reproduce the issue, it happens when pdm tries to use conda for locking but you aren't inside a conda env, I will fix it and release a new version soon.

Meanwhile you can avoid this behavior doing this:

  1. pdm venv create -cn test this will create a conda environment named test
  2. pdm use --venv test this will use the created venv for commands
  3. pdm add --conda "postgresql>12.0"
suhail-singh commented 2 months ago

Thank you for the clarification; that helped.

It makes sense for it to be possible to do pdm use --venv <env-name> in case the user wants to reuse the same conda environment across projects. However, it would help if the pdm venv create -cn <name> invocation also added the environment name in .pdm-python (which is what pdm use --venv <env-name> does). This is because the actual conda environment that is created by the pdm venv create -cn <name> command isn't simply <name>, but rather <name>-<suffix>. As such, the current workflow is:

  1. run pdm venv create -cn <name>
  2. view the result and note the name of the environment: <name>-<hash>
  3. run pdm use --venv <name>-<hash>

If 2 and 3 above could optionally be automated, say by passing an additional flag to pdm venv create, it would help. E.g., if something like -cuse or --conda-use is passed to pdm venv create in addition to -cn, then the created environment is also added to the local .pdm-python file.