Skip to content
Advertisement

Tag: python-import

Struggling with python’s import mechanism

I am an experienced java enterprise developer but very new to python enterprise development shop. I am currently, struggling to understand why some imports work while others don’t. Some background: Our dev team recently upgraded python from 3.6 to 3.10.5 and following is our package structure Now, inside the moduleA.py, I am trying to import subpackage2/moduleZ.py like so But, I

ImportError: cannot import name ‘RightsMaster’ from partially initialized module ‘DATABASE.models’ (most likely due to a circular import)

from DATABASE.models import ModuleMaster, PageMaster, RightsMaster ImportError: cannot import name ‘RightsMaster’ from partially initialized module ‘DATABASE.models’ (most likely due to a circular import) (C:UsersADMINDesktoppython serverDATABASEmodels_init_.py) module_page.py rights_master.py user_rights.py init.py Answer I fixed it I was trying to import ModuleMaster in user_rights.py But it is already connected with Foreign_key in PageMaster

Performance impact in having a single import per line

does anybody know if there is a performance difference between having all imports from one module in a single line vs one per line. For example, having: instead of: I’m trying to convince my team to use reorder-python-imports in our pre-commit hooks and this doubt is the only obstacle that prevents me from adding it. Answer Combining imports is technically

Why did I get ModuleNotFoundError?

I tried the following code to import pprint from pprint module like import matplotlib.pyplot as plt. But I got an error instead. Why did I get this error? Answer Because there’s no module pprint.pprint, only a module pprint. You mean: (From the module pprint import the name pprint as p.)

Import module dynamically

I have a program that supports different communication protocols. The idea is, that the user chooses the protocol during startup and then the module supporting the respective protocol is imported. The function below populates the protocol module with all functions and variables from the user-chosen protocol. protocol.py: name is provided by the user at program startup. The issue is, that

3 level chained python import is failing

I have the following folder structure across 3 levels: folder1 contains folder1_1 which again contains folder1_1_1. Now we have a .py file in each folder (besides the ‘init.py’ which is apparently needed, and sorry I don’t know how to put those 2 lines before and after). File contents file1.py file1_1.py file1_1_1.py So each file imports from the directly lower folder.

Import a single file from another directory

I’m kind of new with python and there is something troubling me. I’m using pandas to read an excel file. All works well if I have the excel in the same directory as my .py file. What I want to know is what is the best way to get a file that is in a completely different path. I’ve searched

Advertisement