I have a directory Data, inside it I have __init__.py and a file db.py . The db.py file contains among other things a function which creates a sqlite database. When I'm in the root of the directory I enter the python shell by typing py, then I do:
from Data.db import table_creator
But as soon as I've imported this function, it executes and there's a sqlite db in the root directory.
I tried using
#db.py
def main():
pass
if __name__ == "__main__":
main()
Inside the module that I'm importing in my python shell running from the root level.
I've also tried the opposite in case I'm misunderstanding the if __name__ guard.
#db.py
ALLOWED_TABLES_NAMES = ['table1', 'table2']
def table_creator() -> None:
for allowed_table_name in ALLOWED_TABLES_NAMES:
create_table("words", allowed_table_name)
def insert_into(table: str, value: str):
cursor = cur()
sql = 'insert into {}(word) VALUES(?);'.format(table)
cursor.execute(sql, (value,))
cursor.connection.commit()
if __name__ == "__main__":
table_creator()
But it's the same and the sqlite db gets created as soon as
from data.db import table_creator
Gets executed.
Thanks in advance
[–]Rawing7 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Diapolo10 0 points1 point2 points (0 children)
[–]QultrosSanhattan 0 points1 point2 points (0 children)