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] (https://django-extensions.readthedocs.org/en/latest/runscript.html). The script will run successfully as follows:

python manage.py 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 manage.py 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 'manage.py' is located. The script 'mycomplexscript.py' 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_':

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    scripts
        __init__.py
        mysimplescript.py
    myapps/
        __init__.py
        mycomplexscript.py
        common/
            __init__.py
            comparison.py
            serialisation/
                __init__.py
                serialisation.py
                xml.py
            types_/
                __init__.py
                enum.py

Django-extensions requires that 'mycomplexscript.py'

Answers


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 (+++).

myapps/mycomplexscript:

# Procedures
def run(*script_args):
    import os
    import sys
    import time
    sys.path.append('/usr/tester/SwFact/isg_cid-system_test_current/   
    my_site/myapps')
    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 manage.py runscript myapps.mycomplexscript --script-args Testing 123

/usr/lib/python2.7/site-packages/Django-1.9.2-py2.7.egg/django/core/ 
management/base.py:265: RemovedInDjango110Warning: OptionParser usage for  
Django management commands is deprecated, use ArgumentParser instead
RemovedInDjango110Warning)
('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

Search




Leave an answer