benwad benwad - 2 months ago 4
Python Question

Importing my package from a Django management command

I've written a package which was originally a command-line tool, but I've decided that for Django it should be run from a management command. I've installed my external package (called

codequal
) using
pip install --editable
, and I can successfully use
manage.py shell
to import a module from that package:

in[0]: from codequal import something
in[1]: something.some_method()
out[2]: u'result'


This works fine. However, when I try to do the same thing in a management command, I run into an error:

File "/home/path/to/django/project/some_app/management/commands/codequal.py", line 8, in <module>
from codequal import something
ImportError: cannot import name something


Why is this? I can use other installed packages from management commands. Could it be something to do with my setup.py? I can post snippets from that if needed. Mainly I'm wondering if this part is to blame:

entry_points={
'console_scripts': [
'codequal = codequal.cli:main',
],


Does this prevent from the module being imported from certain places? I can't see how it would, since I can do it from
manage.py shell
.

Answer

The problem is that your file (codequal.py) has the same name that the module. You need to change one of them. I recomended the file inside the app:

/home/path/to/django/project/some_app/management/commands/codequal.py

to

/home/path/to/django/project/some_app/management/commands/codequal_utils.py
Comments