Python Forum
confusion on importing modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
confusion on importing modules
#1
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_URL
according 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.
Reply
#2
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. Try
import webbrowser
raise RuntimeError(webbrowser)
to see if Python imports the standard library's webbrowser or your webbrowser
carter187 likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
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'
Reply
#4
(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.
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
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) λ
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing modules from different folders Tomli 3 1,475 Jun-26-2022, 10:44 AM
Last Post: snippsat
  Importing modules issue mp3909 9 3,565 Jun-24-2020, 10:07 PM
Last Post: snippsat
  Importing Custom Modules in Python 3 Flexico 1 2,597 Aug-24-2019, 08:11 PM
Last Post: snippsat
  Trouble importing modules on a new python version snackman_barry 2 2,577 Jul-12-2019, 11:15 AM
Last Post: snackman_barry
  importing modules PiaNa 1 1,971 Jun-24-2019, 12:50 PM
Last Post: ichabod801
  Importing all modules and using it rohitnirantar 2 2,557 Aug-28-2018, 08:15 PM
Last Post: snippsat
  Importing modules SBachar 2 3,063 Apr-06-2018, 09:08 PM
Last Post: snippsat
  Importing modules Pistolpete 2 2,716 Nov-29-2017, 05:24 PM
Last Post: nilamo
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,464 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020