Python in 2023 still sucks at importing modules from another folder
stackoverflow.com
stackoverflow.com
Relative imports and mucking with sys.path should be avoided. This StackOverflow answer is providing the right recommendation: make your code into a library. You can do this via pyproject.toml or setup.py, and it's really quite painless. The sooner you do so, the easier things will be.
If you really just want to hack something together (and aren't planning on sharing the code with anyone), you can use importlib to import a Python source file directly [0].
0: https://docs.python.org/3/library/importlib.html#importing-a...
> Otherwise please use the original title, unless it is misleading or linkbait; don't editorialize.
PyScript seems to take away the pain of using YAML and all the boilerplate created to make it so people in the system don't have to program. But there is just so much you have to know about the quirks of the language on top of the quirks of the author's specific choices.
Say I name my package "foo", foo is the top level folder for the source code. Inside foo there is a "main.py" file as the project's start point, and other various modules, let's have one called "module1.py". Now, add another package under "foo", the obvious example name here would be "bar", and add another module under "bar" called "module2.py".
The project structure looks like this:
- foo/
|- main.py
|- module1.py
|- bar/
|- module2.py
To reference "module1.py" in "module2.py", just write import foo.module1
To reference "module2.py" in "main.py", do as follows import foo.bar.module2
There is no problem importing from any level.To start the program from command line, enter:
python -m foo.main
It should not be so complicated.
Coming from Python, I got used to the "explicit is better than implicit" Zen. With "import *" being discouraged, usually when you see something in your Python code, you know exactly where it came from. When I started out with golang, seeing references to things that came out of nowhere (to my eyes) annoyed the hell out of me. I still consider this a cognitive burden.
Generally in Go: functions, structs, and consts are namespaced in a way I think is familiar to most, by package origin.
If something is not, then it's either a function/struct in the same namespace (same file, or directory), variable in same scope, or it's a built-in.
Built-ins like `append`, are not much different than say `map`, `filter`, `max`, etc in Python.
Anything sourced from a subdirectory, parent directory, or sibling directory needs to be explicitly imported via the module path.
In Ruby people achieve something similar by convention (explicitly creating a namespace and keeping the names in sync with the file name), but not everyone follows convention and things can turn into a mess.
If I had to suggest one flaw in Ruby it would be this, in many other ways I find it nicer to code in then Python, but honestly this one thing is enough to really sour the experience.
Discoverability in Ruby is usually done via IRB, and you can call "source_location" on a method to find where in code it the method is defined (or redefined, as can happen in Ruby)