473,378 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Load objects using __import__

I load objects from a file (to django framework _but it's a python
question_).

1) Before I used this
from project.I18n.models import *

And to load the object
c1 = Country(name="Afghanistan")

But the problem is that there is to edit 'project' manually.

2) So I setup the environment and import the module:
project_directory = os.path.abspath('')
project_name = os.path.basename(project_directory)
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings" % project_name
sys.path.append(os.path.dirname(project_directory) )
module = __import__('%s.I18n.models' % (project_name), {}, {},
[''])
sys.path.pop()

It's all correct until here and to checking it I run:
print i18n_module
<module 'project.I18n.models' from
'/home/lala/django/project/I18n/models.pyc'>

print i18n_module.Country
<class 'project.I18n.models.Country'>

But when I load the object I get
SyntaxError: invalid syntax

c1 = <module 'project.I18n.models' from
'/home/lala/django/project/I18n/models.pyc'>.Country(name="Afghanistan")

Any idea?

Nov 21 '06 #1
2 1563

MindClass wrote:
But when I load the object I get
SyntaxError: invalid syntax

c1 = <module 'project.I18n.models' from
'/home/lala/django/project/I18n/models.pyc'>.Country(name="Afghanistan")
It looks like you're running generated code somewhere, but passing the
module itself rather than the name of the module to the code generator
by mistake. Perhaps you're execing some code that you pass the module
name to? Something like this?

exec 'c1 = %s.Country(name="Afgahanstan")' % module_name

You seem to be erroneously using the module itself, not the module
name.

That's about all I can help without more information.
P.S. if you are doing that, consider using getattr instead of exec.

c1 = getattr(module,Country)(name="Afgansitan")
Carl Banks

Nov 21 '06 #2
Indeed before reading your message I solved it doing:

model_name = Country
mod = eval('i18n_module.%s' % model_name)
c1 = mod(name="Afghanistan")
exec c1

but I'll use getattr as you say. Thanks

Carl Banks ha escrito:
MindClass wrote:
But when I load the object I get
SyntaxError: invalid syntax

c1 = <module 'project.I18n.models' from
'/home/lala/django/project/I18n/models.pyc'>.Country(name="Afghanistan")

It looks like you're running generated code somewhere, but passing the
module itself rather than the name of the module to the code generator
by mistake. Perhaps you're execing some code that you pass the module
name to? Something like this?

exec 'c1 = %s.Country(name="Afgahanstan")' % module_name

You seem to be erroneously using the module itself, not the module
name.

That's about all I can help without more information.
P.S. if you are doing that, consider using getattr instead of exec.

c1 = getattr(module,Country)(name="Afgansitan")
Carl Banks
Nov 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marco Aschwanden | last post by:
Hi I have a script that looks for modules and tries to load them. After loading I attach the "server"-API function to it. module = __import__(module_name, globals(), locals(), ) module.server...
2
by: Michel Perez | last post by:
Hi, am very newbie in Python, but as part of a project i need to load configuration -a settings.py file in the package dir- of my apps recursively, something like this: ...
2
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
Query about the __import__ function ~pwd /home/preyan/test ~ls sample.py ~python >>> mod=__import__('sample') >>> amod=__import__('/home/preyan/test/sample') >>>
0
Sl1ver
by: Sl1ver | last post by:
i know how to get the id of the current selected row in the grid. but i now would like to know as soon as someone adds a new line ends edit, then my sql executes to load the other grid where id =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.