orator
orator copied to clipboard
Seeder : [Import Error] No module named ......
Hi,
I'm trying to run a db:seed but it always return an ImportError. I'm using Orator via flask-orator package.
The error :
__[ImportError] No module named customer_seeder__
My project directory:
- app
- migrations
- seeds
- __init__.py
- customer_seeder.py
- database_seeder.py
This is the trace :
Exception trace:
/home/imanovski/www/rimantoro/labs/python/learntofly/local/lib/python2.7/site-packages/cleo/application.py in run() at line 96
status_code = self.do_run(input_, output_)
/home/imanovski/www/rimantoro/labs/python/learntofly/local/lib/python2.7/site-packages/cleo/application.py in do_run() at line 188
status_code = command.run(input_, output_)
/home/imanovski/www/rimantoro/labs/python/learntofly/local/lib/python2.7/site-packages/cleo/commands/command.py in run() at line 90
return super(Command, self).run(i, o)
/home/imanovski/www/rimantoro/labs/python/learntofly/local/lib/python2.7/site-packages/cleo/commands/base_command.py in run() at line 144
status_code = self.execute(input_, output_)
/home/imanovski/www/rimantoro/labs/python/learntofly/local/lib/python2.7/site-packages/orator/commands/command.py in execute() at line 39
return self.handle()
/home/imanovski/www/rimantoro/labs/python/learntofly/local/lib/python2.7/site-packages/orator/commands/seeds/seed_command.py in handle() at line 36
self._get_seeder().run()
/home/imanovski/www/rimantoro/labs/python/learntofly/local/lib/python2.7/site-packages/orator/commands/seeds/seed_command.py in _get_seeder() at line 48
mod = load_module('seeds.%s' % name, seeder_file)
/home/imanovski/www/rimantoro/labs/python/learntofly/local/lib/python2.7/site-packages/orator/utils/__init__.py in load_module() at line 25
mod = imp.load_source(module, path, fh)
/home/imanovski/www/rimantoro/labs/python/learntofly/api_with_flask/seeds/database_seeder.py in <module>() at line 3
from .customer_seeder import CustomerSeeder
db:seed [-d|--database DATABASE] [-p|--path PATH] [--seeder SEEDER]
My customer_seeder.py
from orator.seeds import Seeder
class CustomerSeeder(Seeder):
def run(self):
"""
Run the database seeds.
"""
self.db.table('customer').insert({
'email': '[email protected]',
'username': 'imanovski',
'mobileno': '0867999999',
'password': 'mypassword',
'fname': 'First',
'lname': 'Name',
'image_path': none,
'status': 1
})
My database_seeder.py
from orator.seeds import Seeder
from .customer_seeder import CustomerSeeder
class DatabaseSeeder(Seeder):
def run(self):
"""
Run the database seeds.
"""
self.call(CustomerSeeder)
The error is very clear, where I cannot import customer_seeder.py, but do I'm doing this wrong about how to import it using from .customer_seeder import CustomerSeeder ?
I had the same problem, I tried import another seeder by cli and works. I don't know what is the problem, but for the moment In the cli, I make an instance of the database seeder and run() and works :neutral_face:
well... I create in the root of my project a little file with the next
#filename seed.py
from seeds.database_seeder import DatabaseSeeder
d = DatabaseSeeder()
d.run()
now i use python seed.py
npi cual es el problema :v
In Python 2.7, I solved this problem by adding the following code to the seeds/ __ init__.py
file.
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
Then import the other modules in database_seeder.py
with the following code
import customer_seeder
Or you can load the module this way
import os
from orator.utils import load_module
base = os.path.dirname(__file__)
name = 'customer_seeder'
customer_seeder = load_module('seeds.%s' % name, os.path.join(base, '%s.py' % name))