logoalt Hacker News

IshKebablast Wednesday at 7:43 PM2 repliesview on HN

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.


Replies

Jtsummerslast Wednesday at 7:48 PM

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

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

You'd use:

  import ...foo.bar
show 1 reply
ActorNightlylast Thursday at 5:38 PM

>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.

show 1 reply