0

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'
Mehavarnan Murugan
  • 97
  • 1
  • 2
  • 14

1 Answers1

0

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.

Community
  • 1
  • 1
abe
  • 504
  • 4
  • 13
  • @MehavarnanMurugan Will you mark it as the correct answer or just leave it here, dead in the water like the last question you asked? I would appreciate it if you used Stackoverflow correctly. – abe Jan 28 '16 at 04:03
  • 1
    @IbrahimAhmed There are more polite and constructive ways to let someone know what the preferred attitude is in the SO culture (in the same amount of effort as it took to write your comment). Even if they were supposed to know that from reading the FAQ. The OP also holds the right to wait for more answers before he makes an informed decision. And forcing someone to accept an answer would either be frowned upon or look like reputation begging. Read [this](https://meta.stackoverflow.com/questions/288481/what-to-do-if-other-users-are-not-accepting-the-answer) and the duplicate link inside. – Reti43 Jan 28 '16 at 16:16
  • @Reti43 My intensions were not as so, but I see as to how it could be perceived as such. You have a valid point which I will consider following this point. Thank you. +1 – abe Jan 28 '16 at 21:02