Custom Py Module: Intra Module Importing - python

I am creating a custom Python module that I wanted to work similarly to something like numpy where most of the functionality can be accessed by calling np.function() or what have you. However, in creating my submodules and __init__.py file, I think that I've run into a circular import reference. Here's what I'm trying to do and the error I'm seeing, any tips to help me get this working the way I envisioned are greatly appreciated:
Let's call the module "foobar" and say I have a folder named "foobar" on the path and in this folder are three py files: "__init__.py", "foo.py", and "bar.py".
An example of submodule 1, "foo.py":
import bar
class Foo():
def __init__(self):
pass
def runfunc(self):
bar.func()
An example of submodule 2, "bar.py":
import foo
def func():
f=foo.F()
An example of __init__.py:
import numpy
from foo import *
from bar import *
__all__ = ['foo', 'bar']
So what I perceive to be my issue is that when foo is imported, it imports bar, which imports foo, which creates a loop (in my mind). I am also confused as to the use of __init__.py if, when it is called and imports other modules they cannot see each other when executing (i.e. I need to import bar into foo even though __init__.py did this).
When I run something from a script trying to import the entire module, I get the error in bar.py "ImportError: cannot import name foo". I am actually also having this issue elsewhere in the same module where I have a file with the base class "Base" and the extended class "Extended" where base.py needs to import extended.py so it can spawn instances of the expanded class and extended.py needs to import base.py so it can inherit the class.
Thanks for any help in advance!

Related

How to initialize whole Python module and import all classes without needing to reference module name for every call?

Suppose I have a module named 'module1', which has many classes. I want to use all of their classes in my main controller main.py . I can import whole module with import module1, however, I assume that the class names in 'module1' are unique and I do not want to reference module1 every time I call its function such as module1.class1().
How can I import the whole contents of a module without explicitly having to state class/function names and still being able to refer to them without stating the module name.
Such as:
# module1.py.
class Class1():
pass
class Class2():
pass
# __main__.py
import module1
# The following will not work.
object1 = Class1()
object2 = Class2()
You can use the * import pattern
from module1 import *
This lets you use everything inside that module without referencing it.
from module1 import *
instance = Class1()
However, this is generally discouraged. For reasons why, some additional reading:
Why is "import *" bad?
https://www.geeksforgeeks.org/why-import-star-in-python-is-a-bad-idea/

How to build a class into a Python module

I have a class file fil1.py and in this file, there is only one class called f. I want to build this class file into a module for which I've tried organizing my files like this:
foo/
foo/
__init__.py
file1.py # where class resides
setup.py
README.md
LICENSE
In the __init__.py, I have one sentence:
from file1 import f
Then, when I publish on pypi and then install back to my local computer. When I try:
from foo import f
I got an error message:
ImportError: cannot import name 'f' from 'foo'
I also tried
import foo
foo.f
The error message was:
foo has no attribute f
I am not sure how to achieve what I want.
There is no such thing as a "class file" in Python. A file is a module, and modules may contain zero or more classes, functions, or valid python statements not enclosed in a class or function (which run just one time when the module is imported). That said, I think you want:
foo/file1.py:
class f:
[...]
foo/__init__.py:
from foo.file1 import f
then all of the following are valid (assuming foo is available on sys.path, which includes the current working directory automatically as the first entry):
import foo
import foo.file1
from foo import f
from foo.file1 import f
foo is a package (because it contains __init__.py) with one module called file1. Alternatively, you could use a relative import inside __init__.py:
from .file1 import f
but that can get messy and is usually not appreciated in larger projects.
To achieve the absolute import from the package foo, I did is move my class definition into py.py. See Using pip to install single class package without module.pyfile import.
root/
foo/
__init__.py # where class resides
setup.py
README.md
LICENSE
Then I can do
from foo import f #f is the class previously in the file1.py

Import objects from a python module that has the same name as an __init__ import

The project is structured as follows:
dir/
__init__.py
foo.py
Foo.py has a function that uses a local assignment:
"""foo.py"""
BAR = 12345
def foo():
# do something with BAR
My goal is to import the object BAR to use in my own code. However, __init__.py contains an import of bar that masks any attempt to import from foo as a module:
"""__init__.py"""
from dir.foo import foo
So when I interact with the package, I'm only able to see dir.foo as a function definition instead of a module. How can I get access to dir.foo.BAR?
Lame hack:
import sys
foo_module = sys.modules["dir.foo"]
bar = foo_module.BAR
Better way might be to ask the author of dir not to shadow the submodule name within the top-level namespace, for example by avoiding naming a function the same way as the module in which it was defined.

Import module that contains main module functions

Suppose I have this folder structure:
module
module.py
__init__.py
main.py
Main.py imports module.py which itself should have functions that are only present in main.py. For example, main.py code:
from module import *
def foo(var):
print var
module.foo_module()
Content of module.py:
def foo_module():
foo("Hello world!")
Is there anyway I can achieve this without repeating the functions? If not, how can I import main.py into module.py?
Many thanks
Everything is an object in python, including functions. You can pass the necessary function as an argument. Whether this makes sense in your case, I don't have enough information to know.
def foo(var):
print var
module.foo_module(foo)
def foo_module(foo):
foo("Hello world!")
Avoid circular imports. You could do that here by placing foo in module.
If you don't want foo in module, you could instead create a separate module bar to hold foo, and import bar in both main and module.

How do I make classes from a submodule available in a parent module's namespace?

Here's a python module. foo is in sys.path.
foo\
__init__.py
bar\
__init__.py
base.py
class Base(object)
derived.py
import foo.bar.base as base
class Derived(base.Base)
I've got nothing fancy going on yet. If I want to instantiate the Derived class from the derived module, I can do that easily enough:
import foo.bar.derived as derived
print(derived.Derived())
However, I'd like to just import the bar module and call bar.Derived(), because I plan to have lots of classes within lots of different modules, and I don't want to deal with all these tentacular import paths. My understanding is that I can simply import Derived into the namespace of the bar module, by modifying my project like so:
foo\
__init__.py
bar\
__init__.py
from foo.bar.derived import Derived
base.py
class Base(object)
derived.py
import foo.bar.base as base
class Derived(base.Base)
Now I should be able to do the following:
import foo.bar as bar
print(bar.Derived())
But I get an AttributeError complaining that the foo module has no submodule called bar:
test.py (1): import foo.bar
foo\bar\__init__.py (1): from foo.bar.derived import Derived
foo\bar\derived.py (1): import foo.bar.base as base
AttributeError: 'module' object has no attribute 'bar'
In fact, my original test code (at top) doesn't work either! As soon as I try to import foo.bar, I get errors.
What I can gleam from this error is that the import statement in __init__.py causes derived.py to be executed before bar is fully loaded, and therefore it can't import the module (also from bar) which contains its own base class. I'm coming from the C++ world, where ultra-nested namespaces aren't as integral and a simple forward declaration would negate this problem, but I've been led to believe that what I'm looking for is possible and at least a somewhat acceptably Pythonic solution. What am I doing wrong? What's the correct way to make classes from a submodule available in the parent module's namespace?
If you're working with Python 2.5 or later, try using explicit relative imports (http://www.python.org/dev/peps/pep-0328/#guido-s-decision):
test.py (1): import foo.bar
foo\bar\__init__.py (1): from .derived import Derived
foo\bar\derived.py (1): from . import base
(Note that if you are indeed working with Python 2.5 or 2.6, you'll need to include from __future__ import absolute_import in your modules.)
in derived.py, use this:
EDIT: as JAB pointed out, implicit relative imports are deprecated, to the following isn't recommended (although it does work still in Python 2.7 - with no deprecation errors!).
import base # this is all you need - it's in the current directory
Instead, use:
from . import base #
(or)
from foo.bar import base
instead of:
import foo.bar.base as base
This will solve both your errors (since they're from the same issue). Your import doesn't work since there is no base function or class inside the foo.bar.base module.

Categories