AttributeError: module 'secrets' has no attribute 'choice'

365 views
Skip to first unread message

Igor Gnatenko

unread,
Jan 31, 2020, 10:26:13 AM1/31/20
to NetBox
Hello,

I'm trying to package netbox for Fedora, so I install netbox folder into a /usr/share/netbox, do some stuff around permissions and systemd service files and everything seem to work until the point I try to create superuser.

```
[root@konsky-kokot netbox]# sudo -u netbox ./manage.py createsuperuser
Username (leave blank to use 'netbox'): 
Email address: 
Password: 
Password (again): 
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 189, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/models.py", line 158, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/models.py", line 140, in _create_user
    user.set_password(password)
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 98, in set_password
    self.password = make_password(raw_password)
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/hashers.py", line 76, in make_password
    salt = salt or hasher.salt()
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/hashers.py", line 188, in salt
    return get_random_string()
  File "/usr/lib/python3.8/site-packages/django/utils/crypto.py", line 47, in get_random_string
    return ''.join(secrets.choice(allowed_chars) for i in range(length))
  File "/usr/lib/python3.8/site-packages/django/utils/crypto.py", line 47, in <genexpr>
    return ''.join(secrets.choice(allowed_chars) for i in range(length))
AttributeError: module 'secrets' has no attribute 'choice'
```

After quick look, I see that in the `sys.path`, the first entry is `/usr/share/netbox` where `secrets` directory exists with `__init__.py`.

Of course, this is system with Django 3 and many other updated packages (since we use latest versions of modules) from Fedora and not what's written in requirements.txt.

Did anybody hit such issue before? Any ideas how to fix it apart from mangling sys.path from manage.py?

Brian Candler

unread,
Jan 31, 2020, 11:55:33 AM1/31/20
to NetBox
If you enter "secrets.choice" into the Google Groups search box, you'll find your posting plus these two:


That user got exactly the same error. It was because they had installed Django 3.0 instead of the version listed in requirements.txt
(pip3 install -r requirements.txt)
Reply all
Reply to author
Forward
0 new messages