Share this:

All over the world, Python developers deal with the “PermissionError: [WinError 5] Access is denied” on a regular basis. Usually, this error occurs when working with applications made with Python or development environments used to run Python code. 

Further, in this post, I have shared a list of scenarios when a Python developer is most likely to get stuck with this problem. Before we move forward, I would also like to inform you that there are several solutions to this problem, although the error may reoccur in the future. 

When Does the “Permissionerror: [WinError 5] Access is Denied” Occur?

Web developers have reported having encountered this problem several times when carrying out their usual programming-related tasks. It should be noted that the “PermissionError: [WinError 5] Access is denied” is mainly a consequence of a developer trying to fiddle with a directory. 

Attempting to delete a directory may also result in this particular error. With a non-admin user account, you are allowed to remove a file but not a directory (or change the name or its structure).

Here are the typical scenarios when you might see the PermissionError: [Winerror 5] Access is denied:

1. Trying to Run PySpark 

PySpark is a tool created by the Apache Spark community to help Python programmers work with Data-Driven Documents efficiently and quickly. This tool needs to be run on a machine with Java and Scala installed. Although installing PySpark may not be an issue but running it may result in the PermissionError. 

2. When Using Python PIP

PIP is a Python package installer. Developers are denied access when they attempt to install new packages or update the existing PIP application. This can block the progress of a project as third-party packages and APIs are a crucial part of programming with Python.  

3. When Running TensorFlow

Just like PySpark, TensorFlow is a third party program used to enhance the math abilities of a Python application. The Google Brain team developed it. Developers have reported having denied access when installing the TensorFlow package. It turns out, TensorFlow does not go well with some of the other Python packages, especially when the developer has left their session open in the project.

4. When Importing Tesseract 

Tesseract is an optical character recognition engine developed and maintained by Google. It is one of the rare APIs that can equip a Python project with text reading embedded in an image. If you have worked with Tesseract before, you would know that it denies a developer from gaining access, mainly when the project is run on a workplace computer. 

What Are the Easy Ways to Get Rid of “PermissionError: [WinError 5] Access is Denied”?

1. Update Python 

If you are using an older version of Python (older than 3.7.4), it would be beneficial to update it to the latest version. The fourth updated version of Python 3.7 came with a lot of improvements and bug fixes. You will also be surprised to learn that Python has released a newer version called Python 3.8. You can read all about its latest features on the official website

Step 1

Unfortunately, there is no way to override the currently installed version of Python with a new one. You have to install both versions side by side. Or you can uninstall the currently installed version and subsequently install the Python 3.8. 

To uninstall the installed version of Python, simply open the Control Panel and click on Programs. Further, click on Uninstall. Find the relevant program and uninstall it by right-clicking on it. You are now free to install the latest version of Python.

Step 2

You can find the latest version of Python on its official website. Download the setup and place it in any of the usable drives—next, your need to launch the new version with the help of py.exe launcher.  

Step 3

To launch Python, open the Command Prompt window and type the code “py 3.8 <path of root directory>myscript.py.” Hit Enter. You don’t need to fiddle with the environment variable. 

Step 4

Restart the computer and try to carry on with the task that gave you the “[WinError 5] Access is denied” error. 

2. Try Running as an Administrator

If you are being denied access to run a seemingly ordinary Python script, do not worry. Simply run the script as an administrator. What does running as an administrator actually mean? 

When you log on to Windows with a user account, the operating system creates an access token, which signifies your permissions and privileges. Running as an administrator allows you to bypass the restraints (if any) and carry out the task without any hurdles. 

Follow the instructions given below to run a Python script as an administrator.

Step 1

Assuming you need to run the script very often, create a shortcut for it on the desktop (something like pythonScript.exe). 

Step 2

Next, change the target location of the shortcut to one of the scripts. Make sure the filename of the script is appending the filename of the shortcut. 

Step 3

Right-click on the shortcut. Click Properties from the drop-down menu. Under the General tab, go to Advanced. Here you will find the option “run as administrator”. Check the box. Click Apply and then OK.

Step 4

Now you can run the script without worrying about the permission denied error.

3. Check Your Script for Incomplete File Location

Sometimes the root of the problem might lie in the code itself. Generally, compilers do not throw an error or an exception when you set an incorrect path to an external file. Compliers check for syntax errors and sometimes logical errors. 

Setting an incorrect path or file location can be considered as a typing mistake. If your script does involve file input or file output, make sure to double-check the path.

For instance, I came across a Python developer who has mistakenly set an incomplete path to an image. 

Incorrect code

imageVar = obj.imread(‘D:/project/folder_name/image.png’)

Correct code

imageVar = obj.imread(r’D:/project/folder_name/image.png’)

If you look closely, the correct code has the prefix “r” before the actual path.

4. Make Sure the File You Are Trying to Access is Not Open or in Use

Python has several APIs that allow you to work with external files like image, notepad, video, etc. If your script is trying to access (to write, read or modify) any of the files that are either open in another window or in use by another application, you have to close it or free it from use. An IDE is not allowed to make changes or even access a file that is in use.

5. Make Sure the Working Directory Path of Your IDE is Set Right

If you are using a Python IDE such as PyCharm for the first time, it is most likely that you have not set the working directory path yet. By default, PyCharm creates all directories in the python interpreter location itself. You need to set the working directory path to the base project directory. To solve this issue, go to the Run Configurations menu of PyCharm and simply change the file path to that of your project directory. 

Conclusion

Python is one of the most used languages in the world today. From beginner to advanced developers, it is not uncommon to encounter easy-to-solve errors such as “PermissionError: [WinError 5] Access is denied.” This issue is more likely to occur when you are using your workplace computer. An employer is unlikely to trust their employees with an administrator account.

related posts:


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

If you need any help feel free to contact us.