Can a Python module use import from another file?

I have something like this:

 # a.py  
 import os
 class A:
   ...

 # b.py
 import a
 class B(A):
   ...

In class B (b.py), I would like to be able to use modules imported into a.py (in this case, os). Is it possible to achieve this behavior in Python or should I import modules into both files?

Edit: I'm not worried about import time, my problem is the visual clutter that blocks the import block. I get things like this in every controller (RequestHandler):

 from django.utils import simplejson
 from google.appengine.ext import webapp
 from google.appengine.ext.webapp import template
 from google.appengine.ext import db

What I would like to avoid.

+3
source share
6 answers

Yes, you can use import from another file by going to a.os.

, - , , ( ).

→ . sys.modules.

import sys
sys.modules

>>> pprint.pprint(sys.modules)
{'UserDict': <module 'UserDict' from 'C:\python26\lib\UserDict.pyc'>,
 '__builtin__': <module '__builtin__' (built-in)>,
 '__main__': <module '__main__' (built-in)>,
 '_abcoll': <module '_abcoll' from 'C:\python26\lib\_abcoll.pyc'>,
# the rest omitted for brevity

, Python , , . , . sys.modules.

-, .

: - , .

4 5 , . : " , ". , , :

<importheaders.py>
from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db


<mycontroller.py>
from importheaders import *
+13

.

python - . python . .

, :

# module_a.py
class A(object):
    pass

print 'A imported'

# module_b.py
import module_a

class B(object):
    pass

print 'B imported'

# at the interactive prompt
>>> import module_a
A imported
>>> import module_a     # notice nothing prints out this time
>>> import module_b     # notice we get the print from B, but not from A
B imported
>>> 
+1

. , , . :

import os
def x:
   return os

- + .

0

, python. .

0

. .

# a.py
class A(object):
    ...

# b.py
import a
class B(a.A):
    ...

In Django, for example, many of the packages simply import the contents of other modules. Classes and functions are defined in separate files for separation only:

# django/db/models/fields/__init__.py
class Field(object):
    ...
class TextField(Field):
    ...

# django/db/models/__init__.py
from django.db.models.fields import *

# mydjangoproject/myapp/models.py
from django.db import models
class MyModel(models.Model):
    myfield = models.TextField(...)
    ....
0
source

You can shorten it to:

from django.utils import simplejson
from google.appengine.ext import webapp, db
from webapp import template

Secondly, suppose you have these ^ import in my_module.py

In my_module2.py you can do:

from my_module2.py import webapp, db, tempate

Example:

In [5]: from my_module2 import MyMath2, MyMath

In [6]: m2 = MyMath2()

In [7]: m2.my_cos(3)
Out[7]: 0.94398413915231416

In [8]: m = MyMath()

In [9]: m.my_sin(3)
Out[9]: 0.32999082567378202

where my_module2:

from my_module import math, MyMath

class MyMath2(object):

    the_meaning_of_life = 42

    def my_cos(self, number):
        return math.cos(number * 42)

and my_module1:

import math

class MyMath(object):

    some_number = 42

    def my_sin(self, num):
        return math.sin(num * self.some_number)

Cheers, Hope AleP helps

0
source

Source: https://habr.com/ru/post/1708128/


All Articles