[Tutor] ModuleNotFoundError in Terminal, PyCharm OK
trent shipley
trent.shipley at gmail.com
Wed Jun 28 16:53:27 EDT 2023
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
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?
On Tue, Jun 27, 2023 at 3:14 PM dn via Tutor <tutor at python.org> wrote:
> On 28/06/2023 08.35, trent shipley wrote:
> > PyCharm can run the code below or process the marked line, #2, in the
> > console, as in the snip, below, but I can't figure out how to feed the
> > program command line args.
>
> Recommend a review of the way Python keeps track of various and multiple
> directories, and thus how/where it finds the modules for import-ing:
> https://docs.python.org/3/using/cmdline.html
>
> Thereafter, add debug code/create snippet to display the PYTHONPATH, and
> run from (same dir as this) in both PyCharm and terminal.
>
> Illumination will follow!
>
> --
> Regards,
> =dn
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list