How to fix No common-passwords.txt.gz error in Django when trying to createsuperuser

Actually, it did work this time, but it did not solve the problem. I still got the same annoying “FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/.cache/pip/pool/94/b4/cf/common-passwords.txt.gz” error.

Hm. If you uninstall Django, re-install it with pip, then retry the above command, does it work then?

Nope… Didn’t solve the problem either. :confused:

I FINALLY SOLVED IT

Synopsis:
By investigating the problem, it turns out that there’s a file within Django called “password-validation.py”. Within this file, there’s a class called “CommonPasswordValidator”:

class CommonPasswordValidator:
    """
    Validate that the password is not a common password.

    The password is rejected if it occurs in a provided list of passwords,
    which may be gzipped. The list Django ships with contains 20000 common
    passwords (lowercased and deduplicated), created by Royce Williams:
    https://gist.github.com/roycewilliams/226886fd01572964e1431ac8afc999ce
    The password list must be lowercased to match the comparison in validate().
    """

    @cached_property
    def DEFAULT_PASSWORD_LIST_PATH(self):
        return Path(__file__).resolve().parent / "common-passwords.txt.gz"

    def __init__(self, password_list_path=DEFAULT_PASSWORD_LIST_PATH):
        if password_list_path is CommonPasswordValidator.DEFAULT_PASSWORD_LIST_PATH:
            password_list_path = self.DEFAULT_PASSWORD_LIST_PATH
        try:
            with gzip.open(password_list_path, "rt", encoding="utf-8") as f:
                self.passwords = {x.strip() for x in f}
        except OSError:
            with open(password_list_path) as f:
                self.passwords = {x.strip() for x in f}

    def validate(self, password, user=None):
        if password.lower().strip() in self.passwords:
            raise ValidationError(
                _("This password is too common."),
                code="password_too_common",
            )

    def get_help_text(self):
        return _("Your password can’t be a commonly used password.")

What this does is it compares the password provided by the user against an exhaustive list of common passwords (aka. common-passwords.txt), created by Royce Williams, and it’s available on Github, here.

Now, the problem arises when Django tries to locate this file in the directory “/home/runner/.cache/pip/pool/94/b4/cf/” but does not find it.

So, basically the solution is just as simple as creating the file “common-passwords.txt” within your default repl directory:

touch common-passwords.txt

Then copy-pasting the contents of the file (by accessing it on Github in the link above) into this file. Next, you need to gzip the file:

gzip common-passwords.txt

And finally, move it to the said directory where Django likes to find it:

mv common-passwords.txt.gz /home/runner/.cache/pip/pool/94/b4/cf/

Now run “python3 manage.py createsuperuser” again, and voila!! Problem solved.

Now… What I would suggest is that we could raise this issue to the dev team at Replit so that they can incorporate this file within the repl creation process so it comes within each newly created repl from the get-go so we don’t have to go through this tedious process manually every single time.

You’re welcome! :sunglasses:

2 Likes

UPDATE

Actually, upon further inspection, I realised that the file “password-validation.py” is also located within the " /home/runner/.cache/pip/pool/94/b4/cf/" directory only under the name “5596f66c60a28a4706d54df24b366b75cbcf45085884c3def3884c3ad8”.

This somehow explains why you do not need to go through the tedious process I described above to create the “common-passwords.tar.gz” file if you run a Django repl as opposed to a raw Python repl and install Django within it manually.

Furthermore, the problem will also arise if you try to “pip install wagtail” within a Django repl.

But don’t worry, as the solution above can still fix it for sure.

Hmm… :thinking:

=============================
UPDATE 2

To make the process much easier, I created (ChatGPT’ed, actually, but what the heck… :smirk:) a BASH script that downloads, renames, zips, and moves the “common-passwords.txt.gz” file to its correct location. The code is below.

But, before you can use it, you first need to install “wget”. To do that, simply type “wget” in the shell. It will then tell you that wget is not found, and will give you two options:

  • wget.out
  • busybox.out

Obviously, you need to choose “wget.out”.

Now that wget is installed, you need to create the BASH script:

touch download-and-move.sh

Then, open the file in the editor, and paste the following BASH code into it:

#!/bin/bash

# Step 1: Download the file using wget
wget https://gist.githubusercontent.com/roycewilliams/226886fd01572964e1431ac8afc999ce/raw/7e4f976f31f6f9bbd54781c7792f8272fb2fd613/pwnedpasswords-v6-top20k.txt

# Check if the download was successful
if [ $? -ne 0 ]; then
  echo "Download failed. Exiting."
  exit 1
fi

# Step 2: Rename the file to "common-passwords.txt"
mv pwnedpasswords-v6-top20k.txt common-passwords.txt

# Step 3: Zip the file using gzip
gzip common-passwords.txt

# Check if the compression was successful
if [ $? -ne 0 ]; then
  echo "Compression failed. Exiting."
  exit 1
fi

# Step 4: Move the file to the target directory
mv common-passwords.txt.gz /home/runner/.cache/pip/pool/94/b4/cf/

# Check if the move was successful
if [ $? -ne 0 ]; then
  echo "File move failed. Exiting."
  exit 1
fi

echo "Script completed successfully."

NOTE: Feel free to keep a copy of the “download-and-move.sh” file on your computer for future use if you like.

And that’s it! Running “python3 manage.py createsuperuser” now works like a charm! :slight_smile:

2 Likes

@OmarAbdulHafiz, have previously turned off password validation by commenting out
AUTH_PASSWORD_VALIDATORS in settings.py

so that I can continue to experiment with django on replit.

But this is not a solution (unfortunately for us). Password strength validators are basic requirements.

@Replit staff: Could you kindly update on whats the status of your investigations? It should take less than 5 min to create a repl and validate the error (in addition to the public repl provided earlier).

I could not find a way to directly contact replit staff, the options I found invariably led to posting on this forum. Are there options I am missing?

Thanks.

1 Like

@kwloong You may have misunderstood my solution, my friend. :slight_smile:

I did not turn off Password Validation. It will work just fine. See below:

I simply downloaded the common-passwords.txt file that Django uses (which contains all the code/text necessary to be functional), gzipped it, and moved it to the directory where it should be so Django can use it.

Please try it out and let me know if it works for you.

YET… I still totally agree with you that this should not be a final solution. Replit team MUST look into this.

Best regards. :slight_smile:

Hey all!

Can I get a summary of what the issue is and the steps to reproduce it? I’ve seen quite a few updates, but I tried forking one of the Repls shared but wasn’t able to get it to run nor could I repro the error.

2 Likes

Hi, @ShaneAtReplit!

So, here are the steps as you requested:

  1. Create a Python repl.
  2. Install Django
pip install django
  1. Create a django project
django-admin startproject mysite
  1. Move into the new project and migrate
cd mysite
python3 manage.py migrate
  1. Create an admin (superuser)
python3 manage.py createsuperuser

Once you fill in the data (name, email, password), Django will return this error:

Traceback (most recent call last):
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/contrib/auth/password_validation.py”, line 236, in **init**
with gzip.open(password_list_path, “rt”, encoding=“utf-8”) as f:
File “/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/gzip.py”, line 58, in open
binary_file = GzipFile(filename, gz_mode, compresslevel)
File “/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/gzip.py”, line 174, in **init**
fileobj = self.myfileobj = builtins.open(filename, mode or ‘rb’)
FileNotFoundError: [Errno 2] No such file or directory: ‘/home/runner/.cache/pip/pool/94/b4/cf/common-passwords.txt.gz’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/runner/TestErrorInDjango/testErrorInDjango/manage.py”, line 22, in
main()
File “/home/runner/TestErrorInDjango/testErrorInDjango/manage.py”, line 18, in main
execute_from_command_line(sys.argv)
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/core/management/**init**.py”, line 442, in execute_from_command_line
utility.execute()
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/core/management/**init**.py”, line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/core/management/base.py”, line 412, in run_from_argv
self.execute(*args, **cmd_options)
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py”, line 88, in execute
return super().execute(*args, **options)
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/core/management/base.py”, line 458, in execute
output = self.handle(*args, **options)
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py”, line 183, in handle
validate_password(password2, self.UserModel(**fake_user_data))
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/contrib/auth/password_validation.py”, line 50, in validate_password
password_validators = get_default_password_validators()
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/contrib/auth/password_validation.py”, line 22, in get_default_password_validators
return get_password_validators(settings.AUTH_PASSWORD_VALIDATORS)
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/contrib/auth/password_validation.py”, line 36, in get_password_validators
validators.append(klass(**validator.get(“OPTIONS”, {})))
File “/home/runner/TestErrorInDjango/.pythonlibs/lib/python3.10/site-packages/django/contrib/auth/password_validation.py”, line 239, in **init**
with open(password_list_path) as f:
FileNotFoundError: [Errno 2] No such file or directory: ‘/home/runner/.cache/pip/pool/94/b4/cf/common-passwords.txt.gz’

Now, you could be wondering why we need to use a Python repl to work with Django when there is a Django repl template that we can use. But the thing is this issue can also be replicated on the Django repl as well if, for instance, you try to install Wagtail (a Django-based CMS) on it.

1 Like

Besides Omar’s reply to this, please also see

Thanks.

3 Likes

Hi @ShaneAtReplit!

Are there any updates, please?

Thanks

Thank you for this! I will look into this and will follow up once I have an update.

Hey all!

I could reproduce the issue when using a normal Python Repl, but not when I created a Django Repl and created a superuser from that.

Can you try creating a Django Repl first, then following the steps in the README first, and then start on repro step #4 and onward?

Django Repls come preconfigured with Django already set up, including any hidden dependencies or configuration. Python Repls don’t have this extra configuration which is why running some functions of Django won’t work.

Hi Shane,

Thanks for replying, A few queries as follows:

#1
Could not find any documentation on what are the hidden dependencies and configurations made, Could you kindly list down what are these changes, I have repls that are customised already, would prefer to make these changes manually on the repl so they can work properly than recreate a new “Django Repl”

#2
Also, if Django doesn’t work as a package properly (creating a superuser admin is a basic core Django functionality!) , why was it available as a package for install on Replit’s UI (Tools-Packages)? How are we able to differentiate which pacakges are tested to work by Replit?

#3
When using pip in a repl, the packages are downloaded from https://package-proxy.replit.com . I can’t seem to specify an alternative url (using “–extra-index-url option”) so I can install “a proper, official django” . Could you kindly advise how can I specify an external pip repository to install from instead of using Replit’s caches?

Thanks.

1 Like

No problem!

To hopefully answer all of your questions in one go:
I was experimenting with the Django template vs the Python one. I found that because the Python template is using our new Nix Modules system, reverting it back to .replit and replit.nix control, it fixed the issue when installing and using Django.

The Django template does not use Nix Modules yet, which is why it still works properly. So, I have let the team know that Nix Modules have broke Django and will follow up here once I have an update.

Also, we don’t have a way to change the location of the package index as we hardcode pip and poetry to use our package cache. I have asked the team if there are any workarounds or if it is on the roadmap to do so.

breath Lastly,

You can fix the issue with existing Django Repls by copying over the .replit and replit.nix files from the Django template into those Repls. I have tested this on my end and have confirmed that it fixed the issue with the missing file.

3 Likes

@ShaneAtReplit

Could you please explain how to revert it back to .replit and .replit.nix?

Thank you very much.

Do you mean revert the .replit and replit.nix to how they started out in the Repl, or how to set them so Django works?

@ShaneAtReplit I’m asking because when I tried copying over the .replit and replit.nix files from the Django template, as you mentioned above it didn’t work.

And to be honest, the very idea that we need to go through all these cumbersome workarounds seems to defeat the original purpose of replit being an easy-to-use platform with coding, development, etc.

Don’t you think?

I mean this entire thread, we’ve been trying to solve a very basic thing in Django without actually having written a single line of meaningful code at all. :confused:

In any case, I guess I will just stick to my original solution which the bash script I included above. It’s the best solution I could find so far.

Thanks for your efforts, anyways! :slight_smile:

Thanks for the context!

You can restore the files with our History feature.

I completely understand. I have confirmed with the team that this is an issue and will follow up once I have an update on the issue. To confirm, this is not intended, and I apologize for the issues you faced.

As I said above, I have created a ticket and will be following up here once there is an update on the internal ticket!

2 Likes