I Have 4-5 py file all of which uses same import libraries and Login using selenium python, Is their a better way for managing the import statement - TagMerge
4I Have 4-5 py file all of which uses same import libraries and Login using selenium python, Is their a better way for managing the import statementI Have 4-5 py file all of which uses same import libraries and Login using selenium python, Is their a better way for managing the import statement

I Have 4-5 py file all of which uses same import libraries and Login using selenium python, Is their a better way for managing the import statement

Asked 1 years ago
0
4 answers

You can do like this:

# a.py
import time
import selenium

# b.py
from a import time, selenium #You can do like this...

# c.py
from b import * #...or like this

Anyway both the way of doing this are not recommended, you should better import al the needed modules at the beginning of your single files.

Source: link

0

parent/
    __init__.py
    one/
        __init__.py
    two/
        __init__.py
    three/
        __init__.py
module = None
if spec.loader is not None and hasattr(spec.loader, 'create_module'):
    # It is assumed 'exec_module' will also be defined on the loader.
    module = spec.loader.create_module(spec)
if module is None:
    module = ModuleType(spec.name)
# The import-related module attributes get set here:
_init_module_attrs(spec, module)

if spec.loader is None:
    # unsupported
    raise ImportError
if spec.origin is None and spec.submodule_search_locations is not None:
    # namespace package
    sys.modules[spec.name] = module
elif not hasattr(spec.loader, 'exec_module'):
    module = spec.loader.load_module(spec.name)
    # Set __loader__ and __package__ if missing.
else:
    sys.modules[spec.name] = module
    try:
        spec.loader.exec_module(module)
    except BaseException:
        try:
            del sys.modules[spec.name]
        except KeyError:
            pass
        raise
return sys.modules[spec.name]
spam/
    __init__.py
    foo.py
    bar.py
from .foo import Foo
from .bar import Bar
>>> import spam
>>> spam.foo
<module 'spam.foo' from '/tmp/imports/spam/foo.py'>
>>> spam.bar
<module 'spam.bar' from '/tmp/imports/spam/bar.py'>

Source: link

0

Import statement helps in importing code from other files or modules into the file we are working on. You can think of it as a bridge that gives us access to the class attributes or methods from other files so that we can use them. However, there is a proper syntax to follow to do that which we will see in a few seconds. Before that first understand the given directory structure.
application
  |
  |---- module1
  |       |------- __init__.py
  |       |------- file1.py
  |       |------- file2.py
  |
  |---- module2
          |------- file3.py
In this example, first, we will define a class in file1.py and then import it in file2.py.
application/module1/file1.py
application/module1/file1.py
class ArithmeticOperations:

    def __init__(self):
        self.sum_ = 0
    
    def get_sum(self,num1,num2):
        self.sum_ = num1+num2
        return self.sum_
class ArithmeticOperations: def __init__(self): self.sum_ = 0 def get_sum(self,num1,num2): self.sum_ = num1+num2 return self.sum_
application/module1/file2.py
application/module1/file2.py
import file1

obj = file1.ArithmeticOperations()
c = obj.get_sum(3,8)
print(c)

Source: link

0

I'm trying to automate search procedure on facebook. I used the following code
search = driver.find_element_by_xpath('//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input')
search.send_keys('foo') # search for 'foo'
submit = driver.find_element_by_css_selector('#jsc_c_11 > div > a > div > div.hpfvmrgz.g5gj957u.buofh1pr.rj1gh0hx.o8rfisnq > span > span')
submit.click()
Note that I had attempted the same procedure beforehand using
WebDriverWait(f.browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input'))).send_keys("foo")
If it is the facebook Search Facebook which is located on the left corner top side, you can use the belwo css_selector :
input[name='global_typeahead']
code :
wait = WebDriverWait(driver, 10)
search = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='global_typeahead']")))
search.send_keys('foo')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Source: link

Recent Questions on python

    Programming Languages