Python cannot import name - python

I can't seem to figure out how to import two classes to each other. When running the application it simply says
from room import Room
ImportError: cannot import name Room
It might be a design problem but I don't think there's another way to reference the two classes, so this needs to be as it is. The only reason the imports are needed is because they are required by the redisco module in the objects (they need to know the types)
#Room class.
class Room(models.Model):
from player import Player
players = models.ListField(Player, required = True)
#Player class
class Player(models.Model):
from room import Room
room = models.ReferenceField(Room, required = True)
How do I get this to work?
E:
The framework is Redisco (Redis)

Most ORM models either support back references (where the target model of a reference field is given an extra attribute that points back to the referencing object), and / or lets you specify relationships through other means.
Redisco doesn't have back-references that I could discover, but it does support string references. If you pass in a string it'll be interpreted as a model name, matched against the __name__ attribute:
class Room(models.Model):
players = models.ListField('Player', required = True)
This neatly bypasses the import problem altogether.
From the ListField docstring:
target_type -- can be a Python object or a redisco model class.
If target_type is not a redisco model class, the target_type should
also a callable that casts the (string) value of a list element into
target_type. E.g. str, unicode, int, float.
ListField also accepts a string that refers to a redisco model.
The code to resolve the name uses the function get_model_from_key() to resolve the string, which simply searches through all subclasses of models.Model, matching on __name__.
It'll resolve the name when validating new values or when retrieving existing values for the first time, by which time the Player subclass has already been imported.

If they are in the same .py file then you don't need imports. If they are in different files in the same directory just use 'import room' (assuming its in a file called 'room.py'). If they are in separate files in separate directories you will need to use the imp module.
import imp
roomClass = imp.load_source('roomClass', [Path to room.py])
Then it can be called with something like:
aRoom = roomClass.Room(Model)

First of all if you want to import a module to your program, they must be in the same directory.
Follow this for importing modules ;
from filename import classname
Which is the filename= your .py file that you want to import class
class is the specific class that you want to use.
Or;
from filename import *
Which is going to add all functions and classes.
Also check this topic. program is running fine but cannot being import with IndexError

You can reference to this problem.
Pythonic way to resolve circular import statements?
Using import rather than from [...] import [...]

Related

How to import parent class from a separate .py file for building inherited class inside a driver script for Abaqus python (version 2.7.15)?

I have the following file structure
src
|-main_file.py
|-utils
| |-util_debug.py
|-classes
| |-class_coupon_generic.py
| |-class_coupon_fatigue_type_1.py
| |-class_coupon_fatigue_type_2.py
The file class_coupon_generic.py contains a class named coupon_generic which will act as a parent class for different types of coupons in future development. The file class_coupon_fatigue_type_1.py contains a class named coupon_fatigue_type_1 which is inherited from the parent class coupon_generic. I am keeping all the class files in the folder classes. Inside the utils folder, I have a driver script for debugging util_debug.py. I want to import the classes coupon_generic and coupon_fatigue_type_1 (depending on which coupon I am working on) in this file to build the final coupon model.
How do I achieve that?
I tried to import the modules using imp.load_source and importlib.import_module; but I couldn't figure out a way to do that. I am getting some error like name 'coupon_generic' is not defined. If the class coupon_generic is defined inside the file class_coupon_fatigue_type_1.py, then the I can make the import work using imp.load_source, but that kind of defeats the whole purpose of having a parent class. In order to add any new method to the parent class later on, I have to go to separate scripts and modify them individually. What is the correct way of making such import work with classes having inheritance?
Also, I prefer a solution where I can make the import of class_coupon_fatigue_type_1.py from a string name because it will be varying for different coupon types and I can control that by just passing a string name of the file during debugging.

Django cyclic imports and typechecking with mypy

I'm trying to create a simple custom manager with one of my Django models. It causes a cyclic import since I'm trying to import the manager from models.py and the model from managers.py. However, because my manager is creating the model and adding some extra attributes, the type hint for the method is the model instance. I'm having trouble with fixing that type hint since it's not yet imported.
# models.py
from .managers import PublishedBundleManager
class PublishedBundle(models.Model):
data = JSONField()
md5_checksum = models.CharField(max_length=128)
objects = PublishedBundleManager()
The manager has a method to help me create the model instance, but as a convenience, calculates a checksum to fill in during creation. To fix the cyclic imports, I made use of typing.TYPE_CHECKING
# managers.py
import typing as t
from django.apps import apps
from django.db import models
if t.TYPE_CHECKING:
PublishedBundle = apps.get_model(app_label="the_hugh", model_name="PublishedBundle")
class PublishedBundleManager(models.Manager): # Error 1
def create_publish(self, data: t.Dict, description: str) -> PublishedBundle: # Error 2
PublishedBundle = apps.get_model(app_label="my_app", model_name="PublishedBundle")
json_data = json.dumps(data, sort_keys=True)
md5_checksum = hashlib.md5(json_data.encode("utf-8")).hexdigest()
return PublishedBundle.objects.create(data=data, md5_checksum=md5_checksum)
However, I'm getting 2 errors.
Missing type parameters for generic type "Manager" [type-arg]mypy(error)
name 'PublishedBundle' is not defined
I'm fairly new with typed python and never faced this issue before. I understand that 2 is happening because PublishedBundle hasn't yet been defined, but I can't define it because it causes a cyclic import. Can anyone help me out please?
One simple way to work around this is to have the manager and the model in the same file. They are intricately related anyway.

Creating Object With A For Loop

Firstly, I do apologise as I'm not quite sure how to word this query within the Python syntax. I've just started learning it today having come from a predominantly PowerShell-based background.
I'm presently trying to obtain a list of projects within our organisation within Google Cloud. I want to display this information in two columns: project name and project number - essentially an object. I then want to be able to query the object to say: where project name is "X", give me the project number.
However, I'm rather having difficulty in creating said object. My code is as follows:
import os
from pprint import pprint
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
service = discovery.build('cloudresourcemanager', 'v1', credentials=credentials)
request = service.projects().list()
response = request.execute()
projects = response.get('projects')
The 'projects' variable then seems to be a list, rather than an object I can explore and run queries against. I've tried running things like:
pprint(projects.name)
projects.get('name')
Both of which return the error:
"AttributeError: 'list' object has no attribute 'name'"
I looked into creating a Class within a For loop as well, which nearly gave me what I wanted, but only displayed one project name and project number at a time, rather than the entire collection I can query against:
projects=[]
for project in response.get('projects', []):
class ProjectClass:
name = project['name']
projectNumber = project['projectNumber']
projects.append(ProjectClass.name)
projects.append(ProjectClass.projectNumber)
I thought if I stored each class in a list it might work, but alas, no such joy! Perhaps I need to have the For loop within the class variables?
Any help with this would be greatly appreciated!
As #Code-Apprentice mentioned in a comment, I think you are missing a critical understanding of object-oriented programming, namely the difference between a class and an object. Think of a class as a "blueprint" for creating objects. I.E. your class ProjectClass tells python that objects of type ProjectClass will have two fields, name and projectNumber. However, ProjectClass itself is just the blueprint, not an object. You then need to create an instance of ProjectClass, which you would do like so:
project_class_1 = ProjectClass()
Great, now you have an object of type ProjectClass, and it will have fields name and projectNumber, which you can reference like so:
project_class_1.name
project_class_1.projectNumber
However, you will notice that all instances of the class that you create will have the same value for name and projectNumber, this just won't do! We need to be able to specify values when we create each instance. Enter init(), a special python method colloquially referred to as the constructor. This function is called by python automatically when we create a new instance of our class as above, and is responsible for setting up all the fields of that class. Another powerful feature of classes and objects is that you can define a collection of different functions that can be called at will.
class ProjectClass:
def __init__(self, name, projectNumber):
self.name = name
self.projectNumber = projectNumber
Much better. But wait, what's that self variable? Well, just as before we were able reference the fields of our instance via the "project_class_1" variable name, we need a way to access the fields of our instance when we're running functions that are a part of that instance, right? Enter self. Self is another python builtin parameter that contains a reference to the current instance of the ProjectClass that is being accessed. That way, we can set fields on the instance of the class that will persist, but not be shared or overwritten by other instances of the ProjectClass. It's important to remember that the first argument passed to any function defined on a class will always be self (except for some edge-cases you don't need to worry about now).
So restructuring your code, you would have something like this:
class ProjectClass:
def __init__(self, name, projectNumber):
self.name = name
self.projectNumber = projectNumber
projects = []
for project in response.get('projects', []):
projects.append(ProjectClass(project["name"], project["projectNumber"])
Hopefully I've explained this well and given you a complete answer on how all these pieces fit together. The hope is for you to be able to write that code on your own and not just give you the answer!

django-picklefield not deserializing custon class object

I have a model defined with a field using django-picklefield. Everything was working fine till I refactored our codebase for organizational reasons, but now when I load that model from the db I get back a unicode object instead of the unpickled object. Because the class definition moved pickle cannot find the module it needs to import, I traced the error back to here where pickle tries to import a module that no longer exists.
Is there a way I can resolve this import error by pointing pickle to the correct module or somehow fake the existence of that class definition in the module pickle is looking in with other imports or by creating a class of the same name that inherits from the original class that lives elsewhere?
I was able to resolve this by recreating the old file I had deleted and had it contain only a single line that imports the class that pickle was looking for.

SQLAlchemy - How to Filter Properties of Related Classes During Query Without Class Reference?

I struggled with the title for this question so let me just lay out the code:
File A:
class SomeClass(Base):
__tablename__ = 'some_classes'
id = Column(Integer, primary_key=True)
my_awesome_property = Column(Unicode(255))
other_class = relationship('OtherClass', backref='some_class', uselist=False)
File B:
class OtherClass(Base):
__tablename__ = 'other_classes'
id = Column(Integer, primary_key=True)
my_sweet_property = Column(Unicode(255))
some_class_id = Column(ForeignKey('some_classes.id'))
Now, in many cases I would refer to both of these files from a "higher-order" file containing some functions like so:
Higher Order File:
from model.alpha import SomeClass
from model.bravo import OtherClass
from sqlalchemy.orm import sessionmaker
session = sessionmaker(bind=some_engine)()
def some_random_query():
return session.query(SomeClass).join(OtherClass).filter(OtherClass.my_sweet_property=='Mike Bayer\'s cat speaks SQL.').first()
So that's pretty normal, nothing wrong with that... until... I decide I want to a put a function into one of the lower-level files like File A (and avoid circular imports)
Back to File A:
# pretend I imported a session here
def frustrating_situation():
session.query(SomeClass).join(SomeClass.other_class).filter(SomeClass.other_class.my_sweet_property=='Get ready for an exception!').first()
This will throw this bad boy right here:
AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with SomeClass.other_class has an attribute 'my_sweet_property'
Now I suppose that makes sense given what I know of the internals of SQLAlchemy, but I also think from an API standpoint that statement should really work.
Here is how I worked around it:
session.query(SomeClass).join(SomeClass.other_class).filter(SomeClass.other_class.property.mapper.c.my_sweet_property == 'verbose, yet it works as desired').first()
So after all that, my question is really quite simple: Does anybody know a better / more idiomatic / proper / less dirty feeling way of doing this?
Suggestions welcome.
Side Note
For anyone who is wondering:
"Why not just import the class you want the reference to for the join/filter operation?"
There are a couple reasons why you might not want to / be able to import the class into the module where you are writing the query.
You have split up your class definitions across many files and decided to avoid circular imports by strictly not importing across same-level modules
You have decided to place functions that operate on 1 or more classes not currently defined or imported in the current module and do not wish to import them because they are not used for any other reason in the module (and see reason 1 again).
SomeClass.other_class.my_sweet_property
doesn't work in sqlalchemy. sorry.
You are referring to OtherClass, in this .filter() clause. how you arrive at that name is your business, but the clearest way, from the point of view of what each statement means and where the arguments come from is still just to import things.
edit: A common cause of problems with circular imports occurs when you try to import the names out of modules directly instead of just importing the modules. If you turn code that looks like:
from foo import Bar
def baz():
Bar.quux()
you'll have an import problem if foo is also trying to import this module (say, because it wants to use baz).
Fix it by importing only the module:
import foo
def baz()
foo.Bar.quux()
since foo.Bar is resolved later, only when baz() is called, you don't have any trouble when this module gets imported, since it doesn't actually try to use the contents of any of the modules it imports.

Categories