Tuesday, April 25, 2023

Re: python manage.py commonds: AttributeError: 'PosixPath' object has no attribute 'startswith'

This error is being triggered by an outdated and badly behaved module that contains the run_cmdb_worker management command.

Anything that relies on the presence of BASE_DIR in settings is bad behaviour. This is an entirely arbitrary variable name that might not even be present in a non-default configuration, and it effectively a "private" variable.

There are better ways to determine the Django base directory (although reliance on same is also questionable as it probably assumes a specific Django layout, which is also poor behaviour).

If you insist on using it, then simply wrap BASE_DIR def with a str() cast, or use .as_posix() member, i.e.

BASE_DIR = str(Path(__file__).resolve().parent.parent)

This may invalidate other uses in your settings module though, for example:

BASE_DIR / 'someothervalue'

If your change does not trigger an error otherwise, then you are probably fine.

Regards,
David


On Mon, Apr 24, 2023 at 2:21 AM DL <clouddebuger@gmail.com> wrote:
Django 4.2
Python 3.10.10

# python manage.py run_cmdb_worker
Traceback (most recent call last):
  File "/usr/local/python/lib/python3.10/pkgutil.py", line 417, in get_importer
    importer = sys.path_importer_cache[path_item]
KeyError: PosixPath('/www/cloudadmin')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/www/cloudadmin/manage.py", line 22, in <module>
    main()
  File "/www/cloudadmin/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/opt/.pyvenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/opt/.pyvenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/.pyvenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 275, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/opt/.pyvenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 48, in load_command_class
    module = import_module("%s.management.commands.%s" % (app_name, name))
  File "/usr/local/python/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/www/cloudadmin/apps/cmdb/management/commands/run_cmdb_worker.py", line 2, in <module>
    from cmdb.scheduler import Scheduler
  File "/www/cloudadmin/apps/cmdb/scheduler.py", line 3, in <module>
    from apscheduler.schedulers.background import BackgroundScheduler
  File "/opt/.pyvenv/lib/python3.10/site-packages/apscheduler/__init__.py", line 1, in <module>
    from pkg_resources import get_distribution, DistributionNotFound
  File "/opt/.pyvenv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3260, in <module>
    def _initialize_master_working_set():
  File "/opt/.pyvenv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3234, in _call_aside
    f(*args, **kwargs)
  File "/opt/.pyvenv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3272, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/opt/.pyvenv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 572, in _build_master
    ws = cls()
  File "/opt/.pyvenv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 565, in __init__
    self.add_entry(entry)
  File "/opt/.pyvenv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 621, in add_entry
    for dist in find_distributions(entry, True):
  File "/opt/.pyvenv/lib/python3.10/site-packages/pkg_resources/__init__.py", line 1988, in find_distributions
    importer = get_importer(path_item)
  File "/usr/local/python/lib/python3.10/pkgutil.py", line 421, in get_importer
    importer = path_hook(path_item)
  File "<frozen importlib._bootstrap_external>", line 1632, in path_hook_for_FileFinder
  File "<frozen importlib._bootstrap_external>", line 1504, in __init__
  File "<frozen importlib._bootstrap_external>", line 182, in _path_isabs
AttributeError: 'PosixPath' object has no attribute 'startswith'

I must midify settings.py, change BASE_DIR, it can run
# BASE_DIR = Path(__file__).resolve().parent.parent 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/033f2f50-b073-4a97-9d66-6cc7f8a18049n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAE5VhgVp2UiGc_Dh0jzcg4hTijXtKk8f0PE8axDpLKbky1xo_g%40mail.gmail.com.

No comments:

Post a Comment