1

I made file called thread.py and if I want to import it, it doesn't work. When I use a filename like cheese.py it works fine

import json
from thread import Thread
class Board:
        name = ""
        shortcode = ""
        threads = []
        url = ""
        api_url = ""
        json = ""

        def __init__(self, board, api_url, json):
                self.shortcode = board
                self.api_url = api_url
                self.json = json
                self.__getPosts()

        def __getPosts(self):
                i = 0
                for thread in self.json[0]['threads']:
                        thread = Thread()
                        self.threads[i] = thread
                        i+=1

thread.py

class Thread:
        def __init__(self):
                i = 1
1
  • changing thread.py filename will help Commented Mar 5, 2015 at 9:04

2 Answers 2

5

A built-in module with the name thread already exists.

>>> import thread
>>> thread
<module 'thread' (built-in)>

When you are trying to import using from thread import Thread it is trying to search for the attribute named Thread which does not exists in the built-in thread module.

>>> hasattr(thread, 'Thread')
False

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).

  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  • The installation-dependent default.

For more here

It is recommended that you use a user defined module name that is different than the built-in module name.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to override this so I don't need to rename my class?
You don't need to rename your class, you have to rename your module thread.py to something like mythread.py or something relevant to you .
-1

Python 3 has a built in module Threading that has a class called Thread

which is causing the conflict, so consider renaming your file to something else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.