I'm currently trying to load a pickled file but I'm getting this error:
AttributeError: 'module' object has no attribute 'Model'
I have the following folder structur
project/
- run.py
- module/
- module.py
- #class Model()
- __init__.py
The class I want to import is a module.Model and
I use this to load the File:
import cPickle as pickle
import module
with open("myModelPath") as mf:
m = pickle.load(mf)
If I run this in the project/module subfolder everything works fine but if I try to run the following in my project/folder I get an error
import cPickle as pickle
import module.module
with open("myModelPath") as mf:
m = pickle.load(mf)
I think the problem is, that my Model is now a module.module.Model. Is there an easy way to fix this?
Related
I'm trying to instantiate a class in a submodule using a string name. I've been trying to follow this SO question unsuccessfully:
Python dynamic instantiation from string name of a class in dynamically imported module
I've created the following directory structure:
__init__.py
mymodule/
├── __init__.py
└── MyClass.py
MyClass.py contains:
class MyClass():
def __init__(self, someparam):
print(someparam)
From python I try the following which produces an error.
getattr(importlib.import_module('mymodule'), 'MyClass')
AttributeError: 'module' object has no attribute 'MyClass'
I've tried most of the other solutions put forth in the referenced question and not gotten any of them to work with this setup.
Here other failed attempts based on answers I've tried to follow to illustrate what I've tried and failed at:
import importlib
module = importlib.import_module('mymodule')
class_ = getattr(module, 'MyClass')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MyClass'
Your code is equivalent to:
from mymodule import MyClass
mymodule doesn't contain MyClass, so you will get an error. You want the equivalent of:
from mymodule.myclass import MyClass
That would be:
getattr(importlib.import_module('mymodule.MyClass'), 'MyClass')
I've been trying to import some python classes which are defined in a child directory. The directory structure is as follows:
workspace/
__init__.py
main.py
checker/
__init__.py
baseChecker.py
gChecker.py
The baseChecker.py looks similar to:
import urllib
class BaseChecker(object):
# SOME METHODS HERE
The gChecker.py file:
import baseChecker # should import baseChecker.py
class GChecker(BaseChecker): # gives a TypeError: Error when calling the metaclass bases
# SOME METHODS WHICH USE URLLIB
And finally the main.py file:
import ?????
gChecker = GChecker()
gChecker.someStuff() # which uses urllib
My intention is to be able to run main.py file and call instantiate the classes under the checker/ directory. But I would like to avoid importing urllib from each file (if it is possible).
Note that both the __init__.py are empty files.
I have already tried calling from checker.gChecker import GChecker in main.py but a ImportError: No module named checker.gChecker shows.
In the posted code, in gChecker.py, you need to do
from baseChecker import BaseChecker
instead of import baseChecker
Otherwise you get
NameError: name 'BaseChecker' is not defined
Also with the mentioned folders structure you don't need checker module to be in the PYTHONPATH in order to be visible by main.py
Then in main.y you can do:
from checker import gChecker.GChecker
lcl
|
|----|
|----enterprise
|----phpoob
|----|----|
|----|----'bank.py'
|----|
|----'__init__.py'
|----'module.py'
this is my file structure
__init__.py-->
from module import LCLModule
__all__ = ['LCLModule']
module.py-->
from phpoob.bank import something
__all__ = ['LCLModule']
class LCLModule(something):
_code here_
these are my files
while firing the command python __init__.py i got following error ImportError: No module named phpoob.bank how shold i overcome this error
i also tried it from .phpoob.bank import something but it gives ValueError: Attempted relative import in non-package
what will be solution for it...?
Looks like you are using Python 2.x. Folder phpoob is not treated as Python module. That's why you can't import phpoob.bank.
Solution #1: Create empty file phpoob/__init__.py After that you will be able to import phpoob and import any file inside.
Solution #2: Upgrate to Python 3.
I'm having an issue with the import statement in Python 3. I'm following a book (Python 3 Object Oriented) and am having the following structure:
parent_directory/
main.py
ecommerce/
__init__.py
database.py
products.py
payments/
__init__.py
paypal.py
authorizenet.py
In paypal.py, I'm trying to use the Database class from database.py. So I tried this:
from ecommerce.database import Database
I get this error:
ImportError: No module named 'ecommerce'
so I try with both of these import statements:
from .ecommerce.database import Database
from ..ecommerce.database import Database
and I get this error:
SystemError: Parent module '' not loaded, cannot perform relative import
What am I doing wrong or missing?
Thank you for your time!
Add your parent_directoryto Python's search path. For example so:
import sys
sys.path.append('/full/path/to/parent_directory')
Alternatively, you can add parent_directory to the environmental variable PYTHONPATH.
I create two files: test.py and test1234.py
test.py contains:
import test1234
t = test1234.test()
test1234.py contains:
class test():
def __init__(self):
When put in the same directory, python test.py runs without error.
However, if I create a directory test1234 and put test1234.py and a blank init.py in this directory, python test.py gives the error:
AttributeError: 'module' object has no attribute 'test'
What do I need to do for test.py to be able to see the test class in test1234.py?
You have to import it through the package, or put it in __init__.py.
import test1234.test1234
t = test1234.test1234.test()