Peter Groves February 2016

python/django-extensions ImportError: No module named common.types_

I have managed to run a simple script located in a 'scripts' folder using 'runscript' from django-extensions. The technique is described here [1] ( The script will run successfully as follows:

python runscript mysimplescript --script-args Testing 123

Should I now move the script to a sub-folder, then it appears I need to specify the full path to the sub-folder:

python runscript scripts.myfolder.mysimplescript --script-args Testing 123

I have written a python script which runs successfully when called directly from the command line within its own project folder. The complex script uses a number of additional scripts located within a plethora of sub-folders.

I now want to call this script within django using django-extensions' 'runscript', which means it's being invoked from the 'mysite/' folder where '' is located. The script '' is not in the 'scripts/' folder but relocated to a separate 'myapps/' project folder, along with the sub-folders and scripts.

I've shown the structure below that includes 'mysite/' and some of the sub-folders/files within 'myapp/', notably the module 'myapp/common/types_':


Django-extensions requires that ''


Peter Groves February 2016

I tried sys.path.append("my/path") as recommended by link above and [4]:(Python sys.path - appending PYTHONPATH) and it seemed to work, so all the paths included in 'from' are now recognized without having to set the full path, as I described previously here (+++).


# Procedures
def run(*script_args):
    import os
    import sys
    import time
    from common.serialisation import XML
    from common.task import Subscriber
    from test_ import TestHost
    from common.filesystem import Directory, File
    from common.tables import Column, Condition, CSVTable, Group
    from string import Template
    from datetime import date, time, datetime, timedelta
    from operator import itemgetter

    print script_args

So the result when running the script from 'mysite/':

python runscript myapps.mycomplexscript --script-args Testing 123

management/ RemovedInDjango110Warning: OptionParser usage for  
Django management commands is deprecated, use ArgumentParser instead
('Testing', '123')

It's not ideal to have to hard-code the path this inside the script, so perhaps it can be passed in as one of the arguments. The only problem with this is that imports appear at the top of the file, before any of the command line arguments are processed within the complex script however at least I know it works in principle.

Post Status

Asked in February 2016
Viewed 2,110 times
Voted 6
Answered 1 times


Leave an answer