Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't depend on pkg_resources #185

Open
ataylor32 opened this issue Apr 28, 2024 · 2 comments
Open

Don't depend on pkg_resources #185

ataylor32 opened this issue Apr 28, 2024 · 2 comments

Comments

@ataylor32
Copy link
Contributor

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: python/cpython#95299

@WhyNotHugo
Copy link
Member

WhyNotHugo commented May 15, 2024

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

@aradfarahani
Copy link

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

  1. Install or Upgrade setuptools:
    Ensure that setuptools is installed and up-to-date. You can do this by running the following commands:

    pip install --upgrade setuptools
  2. Uninstall and Reinstall setuptools:
    If the above step doesn't work, try uninstalling and then reinstalling setuptools:

    pip uninstall -y setuptools
    pip install setuptools
  3. Upgrade pip, setuptools, and wheel:
    Sometimes, upgrading pip, setuptools, and wheel can resolve the issue:

    # For Unix/macOS
    python3 -m pip install --upgrade pip setuptools wheel
    
    # For Windows
    py -m pip install --upgrade pip setuptools wheel
  4. 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:

  1. Add setuptools_scm to your pyproject.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"
  2. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants