![]() |
Problems with importing classes in different folder - 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: Problems with importing classes in different folder (/thread-6153.html) |
Problems with importing classes in different folder - Xeraphim - Nov-08-2017 Hey guys I try to reference the class Database in my unit test. This is the first time I program with python so I hope I explain everything correctly :-) I have a following solution structure: Quote:> main in myApiTests.py I try to import the class Database from myApi.py but i get the following error every time I try: Quote:>Traceback (most recent call last): import sys sys.path.append("../src") from myApi import Database import unittest class BotApiTests(unittest.TestCase): def test_GetUserBalance_WhenUserDoesNotExist_ThenReturn0(self): testDatabase = Database('testDb.db') userName = "testUserName" result = testDatabase.GetUserBalance(userName) self.assertEqual(result, 0) unittest.main()I've also tried sys.path.append("..src")or sys.path.append("..")or from myApi import Databasebut nothing works :-( I have an empty __init__.py in every folderFull Code: main/src/myApi.py: import re import string import urllib.request import sqlite3 import praw class Database: def __init__(self, name='cryptotipbot.db'): print("in __init__ -> ", (name)) self.connection = sqlite3.connect(name, check_same_thread=False) self.database = self.connection.cursor() self.CreateDatabase() self.addressIndex = len(self.database.execute("SELECT * FROM usedAdresses").fetchall()) def CreateDatabase(self): print("in CreateDatabase") self.database.execute("CREATE TABLE IF NOT EXISTS Users (redditUsername TEXT PRIMARY KEY, balance INTEGER)") self.connection.commit() self.database.execute("CREATE TABLE IF NOT EXISTS CommentsRepliedTo (commentId TEXT PRIMARY KEY)") self.connection.commit() self.database.execute("CREATE TABLE IF NOT EXISTS UsedAdresses (adressIndex INTEGER PRIMARY KEY, adress TEXT)") self.connection.commit() self.database.execute("CREATE TABLE IF NOT EXISTS DepositRequests (messageId TEXT PRIMARY KEY, adress TEXT, amount INTEGER)") self.connection.commit() def CreateUser(self, redditUsername): user = self.database.execute("SELECT * FROM Users WHERE redditUsername = ?", (redditUsername,)).fetchone() if not user: self.database.execute("INSERT INTO Users (redditUsername, balance) VALUES (?, ?)", (redditUsername, 0)) self.connection.commit() def GetUserBalance(self, redditUsername): entry = self.database.execute("SELECT * FROM Users WHERE redditUsername = ?",(redditUsername,)).fetchone() if entry: balance = entry[1] return balance else: self.CreateUser(redditUsername) return self.GetUserBalance(redditUsername)main/UnitTests/myApiTests.py import unittest from ..src import myApi class BotApiTests(unittest.TestCase): def test_GetUserBalance_WhenUserDoesNotExist_ThenReturn0(self): testDatabase = Database('testDb.db') userName = "testUserName" result = testDatabase.GetUserBalance(userName) self.assertEqual(result, 0) unittest.main() RE: Problems with importing classes in different folder - Larz60+ - Nov-08-2017 You need to provide __init__.py at top level, and empty __init__.py in src and sub-folders. See: https://docs.python.org/3/tutorial/modules.html#packages The top level __init__.py lets python know where dependencies are located. RE: Problems with importing classes in different folder - Xeraphim - Nov-08-2017 Hmm I already tried to have an empty __init__.py in every folder but that didn't work... Does the __init__.py at top level have to have some content? RE: Problems with importing classes in different folder - Larz60+ - Nov-08-2017 Read the docs. See link in previous post. The one at the top describes all of the dependencies. It is not empty. |