Python is one of the most trending programming languages right now. But, programmers have been complaining about a minor yet stubborn issue – ‘FileNotFoundError: [winerror 2] the system cannot find the file specified.’ There are diverse reasons behind this specific problem while programming. It can be some pesky faults that might lead to this issue.
Additionally, you might be ignoring admin privileges, and the IDE might be turning down your request while compiling the snippet of coding. So, you can check the admin permission after you are satisfied with the syntax of your code.
Lastly, it can be the antivirus that might be restricting the code from successful execution. Whatever the reason is, the following solutions should be appropriate for the ‘FileNotFoundError: [Winerror 2] the system cannot find the file specified’ problem.
Modify the Order of Slashes
Bugs in the code snippets can vary widely. Thus, you have to check the lines of code very carefully and then apply the right solution. Users have confirmed that converting the slashes might fix the ‘FileNotFoundError: [winerror 2] the system cannot find the file specified’ trouble.
Try converting backward slashes (\) into forwarding slashes (/) and check if it eliminates the warning. Or else, you can turn forward slashes into backward ones. Meanwhile, check if you are not skipping any line with slashes that need to be changed. Now, try executing the snippet and observe if it works without error.
Set Up Proper Environment or Platform for the Code Execution
You need to take care of the platform while executing a code. If you are still suffering from the ‘FileNotFoundError: [winerror 2] the system cannot find the file specified’ issue, then follow the instructions below:
- Explore the Anaconda Prompt and enable your environment. Well, this is optional for non-env users.
- You must type the following command: python -m ipykernel install -user.
- After that, you can launch the Jupyter notebook and notice if the issue is still there.
Modify the Environment Variables
Changing environment variables can help fix the ‘FileNotFoundError: [winerror 2] the system cannot find the file specified’ issue. If you are clueless about changing the environment variables, then here’s the guide you should follow:
- Search for ‘Edit the System Environment Variables’ using the Search icon or just by pressing the Start button. Select the exact option from the list of search results.
- You must be at the Advanced tab under the System Properties window. Choose the Environment Variables from the bottom of the window.
- Click the Property option regarding which you want to modify the properties. Next, you need to hit the Edit button.
- The properties can be a list of directories or files. Then, you have to hit the New option to include the variable name and its value for the new system variable.
- After you are done, you need to save the changes by clicking the OK button.
Run Python through CMD
There are chances that the specified file couldn’t get set up in the system path. Thus, you might be experiencing the ‘FileNotFoundError: [winerror 2] the system cannot find the file specified’ error. If you are coordinating with sublime, you can launch a CMD window and execute the following command: Terence>python.
After you have run this command, you might encounter the following message:
C: \Users\Terence>python
Python 3.6.0 9v3.6.0:41df79263a11, Jan 25 2022, 21:44:16) [MSC v.1990 64 bit (AM D64)] on win32
Type “help”, “copyright”, “credits”, or “license” for more information.
>>>
Or else, you might see the following message if the above command was a failure:
C: \Users\Terence>python
‘Python’ is not recognised as an internal or external command, operable program or batch file.
This message means that Windows cannot execute the program because it doesn’t know the exact location of the program file.
Configure the argv Path
This common reconfiguration in the argv Python path can eliminate the ‘FileNotFoundError: [winerror 2] the system cannot find the file specified’ error. Programmers have reported that this solution really worked for them. Go through the following steps:
- You need to navigate to the following file path: /python/ share/ jupyter/ kernels/.
- Launch the ‘kernel.json’ file, and you have to configure the argv python path in the following way:
{
“argv”: [
“C: \Anaconda3\python.exe”,
“-m”,
“ipykernel_launcher”,
“-f”,
“{connection_file}”
],
“display_name”: “Python 3”,
“language”: “python”
}
Finally, you have to open up the Jupyter notebook and try executing the code. Check if the ‘FileNotFoundError: [winerror 2] the system cannot find the file specified’ issue is resolved.
Set the Shell= True Argument
The ‘FileNotFoundError: [winerror 2] the system cannot find the file specified’ error might appear due to not specifying this argument. So, you need to set the Shell= True argument in the run method of the subprocess module. Just add the argument and check if this works properly. If the same argument is there, then you can add Shell= False argument and note if this makes any difference.
Execute the Script with Administrative Privilege
To do this, you should have a shortcut for python.exe. Create the shortcut, and then right-click on it. Choose Properties from the drop-down menu. Locate Target in the pop-up window, and you need to change the Target like this:
C: \xxx\ …\python.exe the_script.py
After this, you need to go to the Advanced option under the Shortcut tab. Check the box beside the ‘Run as administrator’ option. Don’t forget to save changes by hitting the Apply button followed by OK.
Deactivate Antivirus and Firewall
Windows might be blocking the IDE from executing the program due to security reasons. Ensure that you disable the built-in firewall and antivirus program, and then try executing the code. Hopefully, this will end the ‘FileNotFoundError: [winerror 2] the system cannot find the file specified’ issue.