Python Forum
Including classes from folder issue - 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: Including classes from folder issue (/thread-17220.html)



Including classes from folder issue - graham23s - Apr-02-2019

Hi Guys,

What i'm trying to do is execute a few actions on a few websites using selenium, i was going to have my structure like:

sites
--site1.py
--site2.py
--site3.py

Each file in the "sites" folder would be a class, in the main file:

import sys
sys.path.append('/sites')
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

#options = Options()                                                                    
#options.headless = False                                                              
#options.add_argument('start-maximized')                                                
#options.add_argument('disable-infobars')                                               
#options.add_argument("--disable-extensions")                                          
#driver = webdriver.Chrome("chromedriver.exe", chrome_options=options)                

print(sys.path.append('/sites'))
I'm trying to include the "sites" folder, and access the classes inside, in a class i have a basic test message:

def testing():
    print('hello testing!')
To check if it's working or not, i cannot seem to see the best way to include the classes from the folder, any help would be appreciated.

cheers guys

Graham


RE: Including classes from folder issue - Gribouillis - Apr-03-2019

A python file is not a class. You could simply import the submodules one by one
from importlib import import_module
import sites
mods = {}
for name in ('site1', 'site2', 'site3'):
    mods[name] = import_module('.' + name, 'sites')
mods['site1'].testing()