Pycharm Django Console No module named 'django_manage_shell' on WSL

Viewed 1004

I recently started to working on WSL Ubuntu-20.04. There is no problem on running the server. But when I tried to connect the Django Console of Pycharm, I get the error below:

Traceback (most recent call last):
File "<input>", line 7, in <module>
ModuleNotFoundError: No module named 'django_manage_shell'

I checked the consoles interpreter and starting script, tried to add project path, my virtualenv path but nothing worked.

My Django Console Options

This is starting script:

import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run(PROJECT_ROOT)

I had no issues like this when I working on Ubuntu nor Windows. This issue happened with WSL.

So I'm waiting for your helps and thanks a lot for your time.

3 Answers

So, I have a terrible workaround for this, and I will leave it here for fellow travelers passing through this lonely part of StackOverflow. But I hope someone else can do better.

The django_manage_shell module is coming from PyCharm's helpers, and isn't part of the normal Django distribution. When running under WSL, these helpers are not accessible to the python interpreter, and you get the above error.

The best solution I was able to arrive at was to make the code in that helper module accessible in the startup script. Fortunately, you can get at this module (and the fix_getpass module it relies on) with only mild difficulty. The code for each can be found in these spots (depending on your PyCharm installation):

C:\Program Files\JetBrains\PyCharm 2021.2\plugins\python\helpers\pycharm\django_manage_shell.py

C:\Program Files\JetBrains\PyCharm 2021.2\plugins\python\helpers\pycharm\fix_getpass.py

I took the code I found in those modules, and pasted it into the Django Console startup script in PyCharm. I had to reorganize things a bit, and change the call django_manage_shell.run() to invoke the copied-over code instead.

I cannot stress how brittle and gross this approach is, but I haven't found an alternative. If I figure out a better approach, or if JetBrains fixes this on their own, I'll come back and update this answer.

EDIT: Slight improvement to my original solution, which involved a rogue python module on the WSL side. This avoids polluting your project, and constrains the hack to just the PyCharm side of things.

as @Nacho said, it is because PyCharm can't reach the helpers folder on Windows Host machine from within WSL.

You can add the path to the PyCharm helpers folder to your path mapping. consequently, all the helper files will be reachable by pycharm

One way to do that is to:

  1. Open Setting -> Build, Execution, Deployment -> Django Console
  2. add new Path Mapping

local path C:/Program Files/JetBrains/PyCharm 2021.3.3/plugins/python/helpers/pycharm

remote path /mnt/c/Program Files/JetBrains/PyCharm 2021.3.3/plugins/python/helpers/pycharm

  1. Close the console and open it again modify the path above according to your PyCharm version and WSL setup

now PyCharm will be able to locate django_manage_shell and any other

I'm not on WSL but this question came up when I was searching for a solution and Hassaan AlAnsary's answer helped me.

On Linux, running PyCharm 2022.2.1, with Python in Docker, I got this error and solved it by modifying the starting script in Settings -> Build, Execution, Deployment -> Console -> Django Console.

I took the line:

sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])

and I added the location of the helper in the Docker container:

sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS, "/opt/.pycharm_helpers/pycharm"])

and this fixed the problem.

Related