Search code examples
pythonseleniumddt

ImportError: cannot import name 'ddt'


I'm using Python3.5 in Windows with pip version 8.0.2. I installed ddt library using 'pip install ddt'. While using ddt library in code, getting import error. How to get rid of this error?

import unittest
from selenium import webdriver
from ddt import ddt, data ,unpack
import time


@ddt
class Search(unittest.TestCase):
    def setUp(self):

        #some code

    @data(("phones",2),("music", 5))
    @unpack
    def test_searchproducts(self, searchterm, results):

        #some code


    def tearDown(self):

        self.driver.quit()

if __name__ == "__main__":
    unittest.main()



Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.5.4\helpers\pycharm\utrunner.py", line 120, in <module>
modules = [loadSource(a[0])]
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.5.4\helpers\pycharm\utrunner.py", line 41, in loadSource
module = imp.load_source(moduleName, fileName)
File "C:\Program Files (x86)\Python 3.5\lib\imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\murugamx\PycharmProjects\New Project\Selenium Learning\ddt.py", line 3, in <module>
from ddt import ddt, data ,unpack
ImportError: cannot import name 'ddt'

Solution

  • The name of your py file is ddt. This is an error. You cannot name your file after the name of a library that you are importing.

    From the Python Doc:

    When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

    • The directory containing the input script (or the current directory when no file is specified).

    So when you use import, the first place it searches is your current directory. This is why your error occurred.