Solved: ImportError when starting manage.py task for Django Project
I figured out my own problem while writing this question but figured it might be helpful if someone ever runs into the same issue.
Original Problem:
I have a Django project which I'd like to use the Tools > Run manage.py Task option with.
I have used it in the past with this same project and haven't changed any of the manage.py settings recently so I'm not sure what is causing this strange ImportError:
Failed to get real commands on module "cts": python process died with code 1: Traceback (most recent call last):
File "/opt/pycharm-2017.2/helpers/pycharm/_jb_manage_tasks_provider.py", line 25, in <module>
django.setup()
File "/home/ave/venv/cts/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/home/ave/venv/cts/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/ave/venv/cts/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/home/ave/venv/cts/lib/python3.5/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'photo_blog'
What's strange about this error is that photo_blog isn't and never was a module of this particular project but it is a module in another project that I also use PyCharm to work on. The manage.py tool continues to work properly for that project.
Relevant info is that I have configured environment settings for use with my manage.py Task, including DJANGO_SETTINGS_MODULE.
Solution:
I realized I had also set the DJANGO_SETTINGS_MODULE environment variable in my ~/.profile which was overriding the environment variables I had configured in PyCharm for the manage.py task in my cts project. The DJANGO_SETTINGS_MODULE in my bash environment was configured for my photo_blog project which is why manage.py was getting confused when it was trying to run the settings of a different project.
Removing DJANGO_SETTINGS_MODULE from my ~/.profile file allowed my PyCharm manage.py Task to function normally again.
Please sign in to leave a comment.
I had a similar symptom but found it was due to an incorrect setting of the shell path.
Goto File->settings -> Tools (v) -Terminal
and under Application settings shell path I have:
"cmd.exe" /k "C:\Users\joeblow\Projects\project_name\venv\Scripts\activate.bat"
In this path I had the activate path pointing to the wrong virtual env. So when the console started it didn't find my installed libraries.