May i start with saying that the import-module-package system of python is the most abominable feature that I have ever encountered in any language ever.
With that out of the way, i have a project in pycharm with the following structure:
Also in my src folder i have a .bat file named py_run.bat that allows me to run the program from anywhere like this:
in order for my imports to work I am doing them like this (for example inside utils.py):
from Extension import Extension (file Extension.py has a class named Extension)
and this way the project runs from pycharm and from cmd too
But i made pycharm create a test for me (test_one.py):
When i run that i get an error on all my imports that are like that: "from Extension import Extension"
this error: ModuleNotFoundError: No module named 'Extension'
if I change the import to "src.Extension import Extension"
the test runs fine, the program from pycharm runs fine but from the cmd it says
ModuleNotFoundError: No module named 'src'
What is wrong with this language? How can it be so stupid and unintuitive for something so simple to work?
How am I supposed to structure my imports?
Thanks in advance.
I know what you mean. I've struggled with it myself.
I think at the heart of your problem is that the command-line Python is using a different PYTHONPATH.
Beyond that, it's hard to help, since I still fumble around a lot myself. But it sounds like maybe it's PyCharm that needs adjusting.
I'm not familiar with the product, but a good parallel might be Eclipse and Java. In Eclipse, you can mark one or more folders in a project to be source folders and that facilitates the ability to run tests while keeping the actual tests separate from the production code. You might want to see if PyCharm does something similar.
If you haven't already, try PyCharm''s "New Project" wiards and see if any of them generate something like what you want. That will give you a model of what you need.
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners