Y
Y
Yoshinon Eared2018-03-10 07:37:56
Python
Yoshinon Eared, 2018-03-10 07:37:56

All points above import or how to import own modules correctly?

Good day!
There is a similar project structure:

myproject
    api
        product.py
        bitrix.py
        storage.py
    main.py

Each file inside the api folder has stub classes like Product , Bitrix , Storage .
Import option 1 (not working)
Import :
from api import *

products = Product()

Exhaust :
Traceback (most recent call last):
  File "C:\Git\xanalyze\main.py", line 3, in <module>
    products = Product()
NameError: name 'Product' is not defined

Но если судить по stackoverflow, то это должно работать. Но увы.
Import option 2 (not working)
Импорт:
from api import *

products = api.Product()

Выхлоп:
Traceback (most recent call last):
  File "C:\Git\xanalyze\main.py", line 3, in <module>
    products = api.Product()
NameError: name 'api' is not defined

Import option 3 (not working)
Импорт:
import api

products = api.Product()

Выхлоп:
Traceback (most recent call last):
  File "C:\Git\xanalyze\main.py", line 3, in <module>
    products = api.Product()
AttributeError: module 'api' has no attribute 'Product'

Import option 4 (non-working)
Импорт:
import api

products = Product()

Выхлоп:
Traceback (most recent call last):
  File "C:\Git\xanalyze\main.py", line 3, in <module>
    products = Product()
NameError: name 'Product' is not defined

Import option 5 (working)
Импорт:
from api import product, bitrix, storage

products = product.Product()

Работает.
С одним нюансом.
Добавим импорт внутри модуля:
# product.py
import bitrix

Выхлоп:
Traceback (most recent call last):
  File "C:\Git\xanalyze\main.py", line 1, in <module>
    from api import product, storage
  File "C:\Git\xanalyze\api\product.py", line 3, in <module>
    import bitrix
ModuleNotFoundError: No module named 'bitrix'


The challenge is to make the classes as easy to access as possible.
Ad 1 (perfect)
product = api.Product()
Ad 2 (perfect)
product = Product()
Announcement 3 (undesirable)
product = product.Product()

And all other actions not related to the Product class should be hidden. Those. here the Bitrix and Storage classes act more for the logical separation of the code. It is very important to avoid the clutter of creating each class instance in main.py . The question is not even a heap. The final implementation in the main should not touch other classes at all, except for Product . Everything else should happen under the hood inside a Product .
I am also ready to listen not only to ways to solve this problem, but also to the approach to structuring in general.
Each object of class Productrepresents literally some kind of product, with unique fields ( name , uuid , etc.)
The Bitrix and Storage class do not need objects at all (singleton? static?) and perform the role of receiving data from "My Warehouse" and " Bitrix".
In the end, it all aggregates into something like this: In fact, I could put everything in one file, or even several, but without putting them in a separate api folder , but I do not find this way acceptable enough. Thanks for any help in advance!
print (product[0].name) # "Наименование такое-то"

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question