confusion on importing modules - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: confusion on importing modules (/thread-41698.html) |
confusion on importing modules - carter187 - Mar-03-2024 Hello, I want to build a simple web browser following along this https://browser.engineering/http.html and have created a directory like this: webbrowser ---- __init__.py ---- src ---- __init__.py ---- URL ---- test ---- __init__.py ---- test_URLaccording to 6.4 https://docs.python.org/3/tutorial/modules.html this is the way to import submodules but when I do import unittest import webbrowser.src.URL class test_URL(unittest.TestCase): def test_init(self): test_URL = URL("http://www.example.org/index.html") test_URL.print_URL() assertEquals(1,1) if __name__ == '__main__': unittest.main()I get " No module named 'webbrowser.src'; 'webbrowser' is not a package ". I am using apple silicon M2 with mac os Sonoma if that is important. RE: confusion on importing modules - Gribouillis - Mar-03-2024 webbrowser is a poor choice for the name of your package, because it is already the name of a module in Python's standard library. Tryimport webbrowser raise RuntimeError(webbrowser)to see if Python imports the standard library's webbrowser or your webbrowser RE: confusion on importing modules - carter187 - Mar-03-2024 thanks, I get RuntimeError: <module 'webbrowser' from '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/webbrowser.py'>so it seems like the library module is imported. I changed webbrowser to Browser to run this import unittest import Browser.src.URL class test_URL(unittest.TestCase): def test_init(self): test_URL = URL("http://www.example.org/index.html") test_URL.print_URL() assertEquals(1,1) if __name__ == '__main__': unittest.main()and I get ModuleNotFoundError: No module named 'Browser' RE: confusion on importing modules - Gribouillis - Mar-03-2024 (Mar-03-2024, 04:05 PM)carter187 Wrote: ModuleNotFoundError: No module named 'Browser'As explained in the tutorial that you linked above, Python will find a package only if it is on the module search path. Concretely you can achieve this by adding the path to the parent directory of the Browser directory to the module search path. You could follow the reply that I made in this thread Later you'll learn how to write pip-installable packages and install them in editable mode. RE: confusion on importing modules - snippsat - Mar-03-2024 Can also advice to make a virtual environment(build into Python) then search path to folder will work. Example setup. myproject/ │ ├── Browser/ │ ├── __init__.py # Makes Browser a Python package │ │ │ ├── src/ │ │ ├── __init__.py │ │ └── URL.py │ │ │ └── test/ │ ├── __init__.py │ └── test_URL.py │ └── other_project_files/ # Other project files (if any)Start like this: # Make G:\env λ python -m venv myproject G:\env λ cd myproject\ # In Linux source bin/activate G:\env\myproject λ G:\env\myproject\Scripts\activate # Now working see (myproject) G:\env\myproject (myproject) λ # Eg make folder folders/files from command line or use OS file explorer G:\env\myproject (myproject) λ mkdir Browser G:\env\myproject\Browser (myproject) λ touch __init__.py ....Test run. G:\env\myproject\Browser\test (myproject) λ rich test_URL.py # Advice use pytest instead import unittest import Browser.src.URL class test_URL(unittest.TestCase): def test_init(self): test_URL = "https://python-forum.io" self.assertEqual(test_URL, Browser.src.URL.web_url()) if __name__ == '__main__': unittest.main() G:\env\myproject\Browser\test (myproject) λ python test_URL.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK G:\env\myproject\Browser\test (myproject) λ |