Re: ModuleNotFoundError in Terminal, PyCharm OK

trent shipley <[email protected]>
Newsgroups gmane.comp.python.tutor
Message-ID <CAEFLyb+ANWncQ1sAc3EmZMaf88_Hw-sBqwDxXuARWmFeiOjr=w@mail.gmail.com>
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 <[email protected]> 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  -  [email protected]
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.