[Tutor] ModuleNotFoundError in Terminal, PyCharm OK

dn PythonList at DancesWithMice.info
Wed Jun 28 17:58:42 EDT 2023


On 29/06/2023 08.53, trent shipley wrote:
> Hi dn,
> 
> #1 was not very useful, but #5
> https://docs.python.org/3/reference/import.html#:~:text=5.4.5.-,module.__path__,search%20for%20modules%20during%20import.
> was
> quite useful
> 
> I discovered that bash had no PYTHONPATH environment variable, and that

Did you discover this by looking from BASH, or did you use Python (while 
it is running) to survey the paths?

(Python sets it up when the interpreter starts)


> PyCharm had obligingly intelligently populated it.  (And if I changed the
> file structure, I had to manually inform PyCharm I had changed it, even if
> I used the "refactor" feature.)
> 
> I wound up with this:
> 
> # shdroll.py
> import sys
> sys.path.extend(['../../..',               # Important
>                   '../../../src',           # Stuff
>                   '../../../src/core'])     # Here
> from simple_hdroll_cli_parser import SimpleHDRollCliParser
> from src.core import core
> 
> parse_cli_args = SimpleHDRollCliParser()
> kwargs = parse_cli_args.parse()
> 
> if kwargs.add is not None:
>      transform = core.add_currying(kwargs.add)
> elif kwargs.mult is not None:
>      transform = core.multiply_currying(kwargs.mult)
> else:
>      transform = None  # not strictly necessary, but it makes my brain hurt
> less
> 
> # Create a die
> die = core.IntegerDie(transform_fn=transform,
>                        sides=kwargs.sides,
>                        bottom=kwargs.base)
> 
> 
> *
> 
> *
> 
> *
> 
> 
> 
> print(rolls.to_string())
> 
> -------
> 
> I also wound up with a src.py file in the ./hackable_dice_roller/src
> directory so it would stop complaining about a missing module in the src
> package.  src.py has one line.
> 
> from src.core import core
> 
> Was that what you had in mind, or did I come up with a singularly ugly and
> unPythonic solution?

Yes it does seem ugly, doesn't it. However, that is a good sign. The 
very next sentence should be a question: "is there a better way?".

Congratulations! (?) You are now ready to discuss Modules 
(https://docs.python.org/3/tutorial/modules.html) which will lead into 
"packages", and the exact scenario under discussion...

NB whilst much discussion centers around mechanisms to deliver (your) 
libraries to A.N.Other, you can also be your own 'customer'.


That said, there does seem to be many 'layers' of directories. If you 
suspect so, perhaps you could draw-up a chart to show how the code-units 
have been separated/laid-out, to aid discussion?

-- 
Regards,
=dn


More information about the Tutor mailing list