pythoncondajupyterpython-packaging

How to install a local folder as a package using `conda`? (yet another relative imports question)


High Level problem

I experiment with/test the Python code I am writing in "playground scripts".

Usually I don't keep these playground scripts around, but recently I have been finding some value in saving them for longer term use. So I decided to create a separate

Problem Setup

I have a conda environment active myproject.

My working folder looks like:

~/x
  __init__.py
  something.py

  ~/x/playground
    playscript.py

I would like to import something.py in playscript.py. I run playscript.py as an interactive window using Jupyter.

If I try to relative import something.py, that fails with:

{
    "name": "ImportError",
    "message": "attempted relative import with no known parent package",
    "stack": "---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
File ~/x/playground/playscript.py:1
----> 1 from .. import something

ImportError: attempted relative import with no known parent package"
}

conda develop (as of 2024 JUN) is not a solution: Deprecate or remove conda develop. Reading through that, multiple people seem to suggest:

pip install --no-build-isolation --no-deps -e .

Questions

Suppose I make changes to ~/x/something.py, or add entirely new modules...do I need to pip install --no-build-isolation --no-deps -e . again? If so, then this is not a good solution.

Related issues

See also the related: "develop" mode? #695


Solution

  • What finally worked: don't use conda

    Don't use conda, miniconda, etc. at all unless you really cannot avoid it. Prefer to use poetry, or rye. I had a smoother experience with rye ultimately.


    Other things I tried that worked to various extents.

    Solution using Interactive Windows, eschewing sub-folders entirely

    Don't bother with creating a folder.

    Create an interactive window within the context of the folder ~/x, and then save it as something.ipynb. The .ipynb extension gives away the fact that:

    Partial Solution using pip -e (doesn't fully work)

    (something still isn't working, as my autocomplete cannot find the relevant module, nor can I import it in interactive mode)

    The pip command suggested is:

    pip install --no-build-isolation --no-deps -e .
    

    The relevant flag on the pip command suggested is -e: https://stackoverflow.com/a/35064498/3486684

    Note that a simple pyproject.toml will have to be created under ~/x:

    [project]
    name = "x"
    authors = [{ name = "Your Name", email = "you@yours.com" }]
    version = "0.1.0"
    requires-python = ">=3.11"
    dependencies = []
    
    [project.optional-dependencies]
    dev = []
    

    Otherwise, pip will complain about there being no setup.py or pyproject.toml around.