[Tutor] ModuleNotFoundError in Terminal, PyCharm OK

trent shipley trent.shipley at gmail.com
Tue Jul 11 17:26:42 EDT 2023


It took me forever to figure out I could use the Python path to call the
executable on the command line instead of the file path.  That was after
getting an error message which led to the great insight that all Python
package examples were singly rooted with a single source--while I was
trying to have sibling src and tests directories.  I would up using
setuptools in an effort to get absolute Python path names to work.  I'm not
sure it was needed, but absolute Python path names for modules are now
working.

Please confirm the setuptools build artifacts should go in .gitignore.

Kivy has been provisionally selected as my GUI framework.  I'm working
through the documentation and tutorials prior to jumping into the RPG-GUI
version.  (There is no demand for that.  The CLI suits my needs, but the
RPG-GUI is a personal stretch goal.)

(venv) trent at trent-virtual-machine:~/pythonworkspace/hackable_dice_roller$
python3 -m src.cli.shdroll --base 1 --sides 6 --dice 3 --rolls 3
   d6_0  d6_1  d6_2  3_*_d6_roll_total  grand_total
0     1     4     4                  9           37
1     1     6     4                 11           37
2     6     6     5                 17           37
(venv) trent at trent-virtual-machine:~/pythonworkspace/hackable_dice_roller$
tree --gitignore
.
├── build
│   ├── bdist.linux-x86_64
│   └── lib
│       ├── api
│       │   ├── core.py
│       │   └── __init__.py
│       ├── cli
│       │   ├── __init__.py
│       │   ├── shdroll_cli_parser.py
│       │   └── shdroll.py
│       ├── gui
│       │   ├── __init__.py
│       │   └── simple_gui
│       │       ├── __init_.py
│       │       └── __init__.py
│       └── tests
│           ├── api
│           │   ├── __init__.py
│           │   ├── test_hdr_core.py
│           │   └── test_hdr_core_visually.py
│           ├── cli
│           │   ├── __init__.py
│           │   └── test_shdroll_visual.py
│           ├── gui
│           │   ├── __init__.py
│           │   └── simple_gui
│           │       └── __init__.py
│           └── __init__.py
├── dist
│   ├── src-0.0.1-py3.11.egg
│   ├── src-0.0.1-py3-none-any.whl
│   └── src-0.0.1.tar.gz
├── hackable_dice_roller.toml
├── README
├── setup.cfg
├── setup.py
└── src
    ├── api
    │   ├── core.py
    │   ├── __init__.py
    │   └── __pycache__
    ├── cli
    │   ├── __init__.py
    │   ├── __pycache__
    │   ├── shdroll_cli_parser.py
    │   └── shdroll.py
    ├── gui
    │   ├── __init__.py
    │   ├── __pycache__
    │   └── simple_gui
    │       ├── __init__.py
    │       └── __pycache__
    ├── __init__.py
    ├── __pycache__
    ├── src.egg-info
    │   ├── dependency_links.txt
    │   ├── PKG-INFO
    │   ├── SOURCES.txt
    │   └── top_level.txt
    └── tests
        ├── api
        │   ├── __init__.py
        │   ├── __pycache__
        │   ├── test_hdr_core.py
        │   └── test_hdr_core_visually.py
        ├── cli
        │   ├── __init__.py
        │   ├── __pycache__
        │   ├── test.csv
        │   ├── test_shdroll_visual.py
        │   └── test.xlsx
        ├── gui
        │   ├── __init__.py
        │   └── simple_gui
        │       └── __init__.py
        ├── __init__.py
        └── __pycache__

32 directories, 46 files

On Wed, Jun 28, 2023 at 3:00 PM dn via Tutor <tutor at python.org> wrote:

> 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
> _______________________________________________
> 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