django-user-sessions
django-user-sessions copied to clipboard
Don't depend on pkg_resources
Expected Behavior
I should be able to run a command like python manage.py shell without any errors.
Current Behavior
[ERROR] management_commands File "/var/www/example.com/manage.py", line 33: Management command error: manage.py shell
Traceback (most recent call last):
File "/var/www/example.com/manage.py", line 31, in main
execute_from_command_line(sys.argv)
File "/var/www/example.com/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/var/www/example.com/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 416, in execute
django.setup()
File "/var/www/example.com/venv/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/var/www/example.com/venv/lib/python3.12/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
^^^^^^^^^^^^^^^^^^^^^^^
File "/var/www/example.com/venv/lib/python3.12/site-packages/django/apps/config.py", line 193, in create
import_module(entry)
File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/var/www/example.com/venv/lib/python3.12/site-packages/user_sessions/__init__.py", line 1, in <module>
from pkg_resources import DistributionNotFound, get_distribution
ModuleNotFoundError: No module named 'pkg_resources'
Your Environment
- Python version: 3.12.3
- Django version: 4.2.11
- django-user-sessions version: 2.0.0
Relevant: https://github.com/python/cpython/issues/95299
pkg_resources is used to read the current package version.
We should probably write the version into a file at build time instead with something like:
[build-system]
requires = ["setuptools>=45", "wheel", "setuptools_scm>=6.2"]
[tool.setuptools_scm]
write_to = "user_sessions/version.py"
version_scheme = "no-guess-dev"
init.py can then use:
from . import version
__version__ = version.version
The error you're encountering, ModuleNotFoundError: No module named 'pkg_resources', typically occurs when the setuptools package is not installed or is not properly configured in your Python environment. Here are some steps to resolve this issue:
Steps to Fix the Error
-
Install or Upgrade
setuptools: Ensure thatsetuptoolsis installed and up-to-date. You can do this by running the following commands:pip install --upgrade setuptools -
Uninstall and Reinstall
setuptools: If the above step doesn't work, try uninstalling and then reinstallingsetuptools:pip uninstall -y setuptools pip install setuptools -
Upgrade
pip,setuptools, andwheel: Sometimes, upgradingpip,setuptools, andwheelcan resolve the issue:# For Unix/macOS python3 -m pip install --upgrade pip setuptools wheel # For Windows py -m pip install --upgrade pip setuptools wheel -
Create a Virtual Environment: If the issue persists, try creating a new virtual environment and installing the necessary packages within it:
# For Unix/macOS python3 -m venv my_env source my_env/bin/activate # For Windows py -m venv my_env my_env\Scripts\activate # Then install the required packages pip install django django-user-sessions
Example Configuration for setuptools_scm
If you need to write the version into a file at build time, you can configure setuptools_scm as follows:
-
Add
setuptools_scmto yourpyproject.toml:[build-system] requires = ["setuptools>=45", "wheel", "setuptools_scm>=6.2"] [tool.setuptools_scm] write_to = "user_sessions/version.py" version_scheme = "no-guess-dev" -
Modify
__init__.py: In your__init__.py, use the following code to import the version:from . import version __version__ = version.version
By following these steps, you should be able to resolve the ModuleNotFoundError: No module named 'pkg_resources' and run python manage.py shell without any errors.