1

I am trying to run a Django project, I tried to import assign_perm and remove_perm from guardian.shortcuts (code listed here: from guardian.shortcuts import assign_perm, remove_perm).

and got an error: ModuleNotFoundError: No module named 'guardian.shortcuts'

I am using python3.8.9 and django2.0.7. I have already successfully tried install guardian(0.2.2) and Django-guardian(2.4.0), and I can import guardian successfully directly. Please help me to figure it out. THX!

P.S. I tried to print(guardian.__file__) and the output is here:

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/guardian/__init__.py
RA.IBOY
  • 69
  • 1
  • 7

2 Answers2

1

ModuleNotFoundError, this error typically occurs when the required package is not installed in your Python environment.

To resolve this issue, follow these steps:

  1. Verify that you have installed Django Guardian: Run the following command to install the django-guardian package:

    pip install django-guardian
    

    Make sure you are using the same Python environment where Django and other packages are installed.

  2. Check your Python environment: Ensure that you are using the correct Python environment where Django and Django Guardian are installed. Sometimes, if you have multiple Python environments or virtual environments, you might be using the wrong one. You can check your current Python environment by running:

    python --version
    

    This should display the Python version installed in your environment.

  3. Check Django Guardian version: Make sure you are using a version of Django Guardian that supports assign_perm and remove_perm functions. In the past, assign and remove_perm functions were available, but later versions replaced them with assign and remove.

    For versions 2.0.0 and above, use:

    from guardian.shortcuts import assign, remove
    

    For versions prior to 2.0.0, use:

    from guardian.shortcuts import assign_perm, remove_perm
    

    Double-check your installed version using:

    pip show django-guardian
    
  4. Restart your application: After making sure you have installed Django Guardian and using the correct import based on the version, restart your Django project or server to apply the changes.

If you have followed these steps and still encounter the issue, please double-check your Python environment, the installed packages, and make sure that the correct versions are being used.

Elie Hacen
  • 372
  • 12
  • really thanks for your detailed answer, I have already followed these steps but still encounter the problem, my Django and Django-guardian are in the same python environment. And for the step3, I tried both but no difference, the error message is that no module named guardian.shortcuts, seems that not related with assign_me and remove_perm. – RA.IBOY Jul 30 '23 at 04:15
  • I tried to import guardian directly and it worked, but failed when I tried to import guardian.shortcuts – RA.IBOY Jul 30 '23 at 04:31
  • Check the project structure: Ensure that your Django project structure is organized correctly, and you have a proper settings.py file. The INSTALLED_APPS in your settings.py file should include 'guardian' like this: python ```INSTALLED_APPS = [ 'guardian', ]``` – Elie Hacen Jul 31 '23 at 05:47
  • 1
    okay this is definitely chatgpt. https://meta.stackoverflow.com/q/421831/11107541 – starball Aug 15 '23 at 08:29
0

emmmm actually I don't know why, I just use pip3 to uninstall guardian and Django-guardian, and reinstall Django-guardian and it worked. and I found that both assign_perm and assign is available for Django-guardian2.4.0, but only remove_perm (rather than remove) is available for this version. If you struggled with this issue and following solutions above still cannot help you can have a try for my solution. Hope this can help you.

RA.IBOY
  • 69
  • 1
  • 7