Skip to content

Experiments to solve my 'Module Not Found' problems with Python.

License

Notifications You must be signed in to change notification settings

rich17lucas/modules_tut

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#README.md This is a trivial project to solve how to update the path when running a Python script that referenced modules, from the command line.

This first started to cause me a problem when I used first Poetry to create a new project for me (though it was the first time I had decided to create a project with modules so I doubt it's problem with Poetry but more my own naivety)

So as an experiment, I created a project with this directory structure

modules_tut:.
├─── __init.py__
├─── foobah
│   ├─── __init.py__
│   ├─── bah.py
│   ├─── foo.py
├─── harrytom
│   └─── __init.py__
│   ├─── harry.py
│   ├─── tom.py
├─── main
│   ├─── __init.py__
│   ├─── main.py

When I executed the main.py the result was a ModuleNotFoundError

(venv) E:\work\pythonprojects\modules_tut>python main/main.py
Traceback (most recent call last):
  File "main/main.py", line 9, in <module>
    from foobah import foo, bah
ModuleNotFoundError: No module named 'foobah'

After Googl-ing & Stackoverflow-ing, I found these articles:

Python Modules and Packages – An Introduction by John Sturtz which was the inspiration for this self learning experiment, and:

Python 3's pathlib Module: Taming the File System by Gier Arne Hjelle which introduced the use of pathlib library

Basically to avoid the module not found error, add these lines to the main program and then its possible to run main.py successfully from the command line.

import os, sys
from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parent.parent))

Now when its executed, it works as expected.

(venv) E:\work\pythonprojects\modules_tut>python main/main.py
arg = a
foo.foo('a'): None
bah.bah(2): 4
harry
tom

About

Experiments to solve my 'Module Not Found' problems with Python.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages