Entire module is not detected by __init__.py

2024/10/14 0:30:53

I have a relatively small python program which is setup like this

Root--Network--DTOLots of py files which contain classes.Other py files in the project

Both in the Network and the DTO folder there is an empty __init__.py.

When I do from Network import DTO, import Network, Import Network.DTO I cannot use the module at all. The debugger says it's completely empty. The root contains the py file I am actually executing.

Both the the __init__.py files in the Network and DTO folder are compiled to pyc while all actual python files aren't.

Does anyone have any idea what I am doing wrong? I am using python 2.7

Answer

For that, you need to import the submodules in your __init__.py. It becomes more difficult to add an import for every submodule if you have many, as you do. In the case of many submodules (and in general), use __all__.

Here's an example for Root/Network/DTO/__init__.py:

__all__ = ['sample_module',...
]for module in __all__:__import__('DTO.%s' % module)

Now you can do from Network.DTO import sample_module. The same idea holds for your other modules, as well.

https://en.xdnf.cn/q/118018.html

Related Q&A

Python output to terminal during ssh login

I have been looking everywhere for this and have not found a solution. I am using python 2.5.1 on an Apple iPod and I am trying to connect to a host with SSH. First I start off with import os. Next I o…

Unable to find SIFT or xfeatures2d in OpenCV Python [duplicate]

This question already has answers here:PyCharm: Installation of non-free OpenCV modules for operations like SIFT, SURF(2 answers)Closed 6 years ago.I recently switch back to python for facial detection…

Django paginate for django 2

I need to use pagination to a Django list but I couldnt find any help online,, only old docs from Django version 1.3 here are my files : views.pydef home(request):all_dress = Item.objects.all().filter(…

AttributeError Button object has no attribute scrlFBtn

from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.core.window import Window from kivy.uix.scrollview import ScrollView from kivy.effects.scrol…

How to set interpreter of WinPython as the vim default python interpreter?

I use a Python distribution named WinPython. Now I want my vim to use the python interpreter in WinPython as its default interpreter. I tried add the F:\WinPython\python-2.7.3.amd64 into my windows env…

how to deal with Python BaseHTTPServer killed,but the port is still be occupied?

I use python BaseHTTPServer,it can handle do_GET,do_POST methods,in do_POST method,i execute linux shell using os.system,when i kill the python script,but the listening port still occupied,so i cant ru…

Circular imports and class fields in python3

Okay, I do understand that this topic is old as hell, but I couldnt find an answer to the particular question that I am asking.Lets say that we have a very simple structure: two files, a.py and b.py, t…

Bad HTTP response returned from the server. Code 500

I have a problem to use pywinrm on linux, to get a PowerShell Session. I read several posts and questions on sites about that. But any that can solve my question. The error is in the Kerberos autenti…

Iterate one list of synsets over another

I have two sets of wordnet synsets (contained in two separate list objects, s1 and s2), from which I want to find the maximum path similarity score for each synset in s1 onto s2 with the length of outp…

Flask werkzeug.routing.BuildError

I doing a flask app and when i try to put a link to redirect a user to his profile page by callingBuildError: Could not build url for endpoint profile. Did you forgetto specify values [business_name]?…