← Back to context

Comment by IshKebab

2 days ago

No it doesn't. You can't do something like `import ../../foo/bar`. You can mess around with PYTHONPATH and importlib to work around that but that's a horrible hack that also breaks all tooling. Not a good idea.

With Deno you can just import by relative file path and it just works like you'd expect and the tools support it. I wish more languages worked like that.

> You can't do something like `import ../../foo/bar`.

https://docs.python.org/3/reference/import.html#relativeimpo...

You'd use:

  import ...foo.bar

  • Common misconception. That doesn't import by relative file path; it imports by relative package path. It's actually quite different. Scripts you run directly aren't in a package so you can't use it at all. You can go above the package root. Namespace packages mean it can jump to other directories.

    Everyone wants that to just mean "import relative to this file" but it doesn't.

>You can mess around with PYTHONPATH and importlib to work around that but that's a horrible hack that also breaks all tooling

....no.

import keyword uses importlib under the hood. It just does a lot of things for you like setting up namespace. But importlib has all the functionality to add the code in any python file cleanly.

My custom agent that I use basically has the functionality to wrap every piece of code it writes as a tool and stores it into python files. During tool calls, it pretty much dynamically imports that code as part of a module within the project. Works perfectly fine.