Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory

The error "Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory" is caused by having a long installation path.

To solve the error, remove the max path limitation.

shell
Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\\Users\\bobbyhadz\\appdata\\Roaming\\Python\\python310\\site-packages\\pip-22.3.dist-info\\METADATA'
Windows has a path limit of 260 characters which often causes issues because longer paths don't resolve.

One way to solve the error is to remove the character limit.

# Removing the PATH character limit

To remove the character limit:

  1. Click on the search bar, type "registry" and click on "Registry Editor".

windows search for registry editor

  1. In the left sidebar, navigate to the "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem" key.

windows long paths enabled

  1. Find LongPathsEnabledKey and double-click on it.
  2. Set the value in the "Value data" field to 1 and click "OK".

windows set long paths enabled to 1

  1. Close the Registry Editor, restart your computer and re-run the pip install command.

Alternatively, you can run PowerShell as an administrator and issue a command.

To run PowerShell as an administrator:

  1. Click on the search bar and type "PowerShell".
  2. Right-click on "Windows PowerShell".
  3. Click on "Run as administrator".

windows run powershell as administrator 5. Paste the following script into PowerShell.

shell
Set-ItemProperty -Path "HKLM:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1
  1. Close the shell, restart your computer and re-run the pip install command.

Alternatively, you can create an empty METADATA file.

# Create an empty METADATA file in the location in the error message

Your error message will contain a path to a METADATA file that pip is looking for:

shell
'C:\\Users\\bobbyhadz\\appdata\\Roaming\\Python\\python310\\site-packages\\pip-22.3.dist-info\\METADATA'

Notice that the path ends with a METADATA file that pip is looking for.

Navigate to the directory from your error message, create an empty METADATA file and rerun the pip install command.

If this didn't work, navigate to the directory from the error message that contains the ...dist-info directory and then move the pip-X.X.X.dist-info directory to another folder.

Once the ...dist-info directory is moved, rerun the pip install command.

The pip install command should recreate the directory and install the specified package.

If this didn't work, try running the pip install command with the --user option.

# Run the pip install command with the --user option

The error is sometimes caused due to not having the necessary permissions to install a package.

To solve the error, run the pip install command with the --user option.

shell
pip install <package-name> --user pip3 install <package-name> --user python -m pip install <package-name> --user python3 -m pip install <package-name> --user

Make sure to replace the <package-name> placeholder with the actual name of the package, e.g. pip install numpy --user.

The --user option installs the package in the user's home directory.

The command basically installs the package scoped to the specific user, not for the entire system. This helps with permission issues.

However, the --user option wouldn't work if you have a virtual environment active.

Another thing that often solves the error is upgrading your pip version before installing the specific package.

shell
python -m pip install --user --upgrade pip python -m pip install numpy python3 -m pip install --user --upgrade pip python3 -m pip install numpy

If that didn't help, run CMD as an administrator and install the package.

# Run CMD as an administrator

To run CMD as an administrator:

  1. Click on the search bar and type "cmd".
  2. Right-click on "Command Prompt".
  3. Click on "Run as administrator".

windows run cmd as administrator

  1. Once you open the shell as an administrator, rerun the pip install command.
shell
pip install <package-name> pip3 install <package-name> python -m pip install <package-name> python3 -m pip install <package-name>
If you use PyCharm, close the IDE, right-click on it and open PyCharm as an administrator and then run the pip install command.

If opening CMD as an administrator didn't help, try to open PowerShell as an administrator and run the command.

To run PowerShell as an administrator:

  1. Click on the search bar and type "PowerShell".
  2. Right-click on "Windows PowerShell".
  3. Click on "Run as administrator".

windows run powershell as administrator

Another common cause of the error is having other Python scripts running in the background when running the pip install command.

Windows throws "EnvironmentError: [Errno 2] No such file or directory" when the file is locked by another process.

If the error persists, change the user's access permissions.

# Change the user's access permissions

The error is often caused because the user doesn't have access to modify the directory where the package should be installed.

To solve the error, allow the user full access to the Python directory.

To change the access permissions for the user:

  1. Open the folder where you installed Python e.g. C:\Program Files (x86)\Python310.

You can find where Python is installed with either of the following 2 commands.

shell
where python python -c "import os, sys; print(os.path.dirname(sys.executable))"
  1. Once you open the directory where Python is installed, right-click on the "Python" or "Python310" folder and click "Properties".

rightclick properties

  1. Go to the "Security" tab and click on the "Edit" button.

click security tab and edit

  1. Allow "Full control" to your user or the entire "Users" group.

allow full control to users

  1. Click on "Apply".
  2. Run the pip install <package-name> command again.

Once you give "Full access" permissions to the user, you should be able to pip install packages without getting any errors.

If you have a virtual environment active, your Python location will be scoped to the specific virtual environment.

In other words, you have to edit the permissions for the user for a Python folder that's located in your project.

You can run the where python command with your virtual environment active to find the directory.

If that didn't resolve the error, try upgrading your version of pip.

shell
# ๐Ÿ‘‡๏ธ If you have pip already installed pip install --upgrade pip # ๐Ÿ‘‡๏ธ If your pip is aliased as pip3 (Python 3) pip3 install --upgrade pip # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install --upgrade pip # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python3 -m pip install --upgrade pip # ๐Ÿ‘‡๏ธ If you have easy_install easy_install --upgrade pip

If you get the error "ModuleNotFoundError: No module named 'pip' in Python", check out my other article:

If the commands from the code sample didn't work for you, click on the "Install pip in Python" link.

After you upgrade pip, upgrade setuptools as well.

shell
pip install --upgrade setuptools pip3 install --upgrade setuptools python3 -m pip install --upgrade setuptools

If that didn't help, try setting include-system-site-packages to true if you use a virtual environment.

# Set "include-system-site-packages" to "true"

If you use a virtual environment, another thing that might help is to:

  1. Open your venv folder.
  2. Click on the pyvenv.cfg file.
  3. Set the include-system-site-packages property to true.
  4. Save the file.
  5. Rerun the pip install command.

include system site packages

# Conclusion

The error "Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory" is caused by having a long installation path.

To solve the error, remove the max path limitation.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev