[Solved] (ModuleNotFoundError: No module named 'conda') when running conda commands within environment

I get this message whenever I try to run conda commands inside an environment with python.

Trigger method:

  1. Run conda create -n myenv python
  2. Run conda activate myenv
  3. Run any conda commands, such as conda activate or conda deactivate or conda install numpy

I think this might be a noob question, but I can’t find much about it. How may I solve this.

Note, I have run conda init zsh. It (should) work, as there are lines about conda appended to .zshrc, however, the command erred like follows:

Traceback (most recent call last):
      File "/usr/lib/python3.11/site-packages/conda/exceptions.py", line 1124, in __call__
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/site-packages/conda/cli/main.py", line 69, in main_subshell
        exit_code = do_call(args, p)
                    ^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/site-packages/conda/cli/conda_argparse.py", line 91, in do_call
        return getattr(module, func_name)(args, parser)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/site-packages/conda/cli/main_init.py", line 33, in execute
        return initialize(context.conda_prefix, selected_shells, for_user, args.system,
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/site-packages/conda/core/initialize.py", line 118, in initialize
        run_plan_elevated(plan2)
      File "/usr/lib/python3.11/site-packages/conda/core/initialize.py", line 714, in run_plan_elevated
        result = subprocess_call(
                 ^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/site-packages/conda/gateways/subprocess.py", line 98, in subprocess_call
        stdout, stderr = process.communicate(input=stdin)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/subprocess.py", line 1209, in communicate
        stdout, stderr = self._communicate(input, endtime, timeout)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/subprocess.py", line 2088, in _communicate
        input_view = memoryview(self._input)
                     ^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: memoryview: a bytes-like object is required, not 'str'

I also have to mention if the environment does not contain python, conda commands can still run normally.

Solved via conda install -n myenv conda

If using system conda, python version must match. However they didn’t, so have to use a duplicate conda.

From the error message it looks as if it’s trying to use a “conda” package installed in your system Python. Did you install that (like with sudo pip install conda or something)? Do you need it? Generally you install conda as a completely separate thing from system Python, so you don’t need or want that conda package installed to the system Python, you just want to run the actual conda (or miniconda, miniforge, etc.) installer.

I didn’t use my pip to install conda, but my system package manager with PYTHON_TARGETS=“python3_11” set.

Okay, but was there a reason you did that? Do you need that package in your system python for some reason?