Меню

Permission denied pycharm ошибка

I get permission denied on pycharm when adding an interpreter. It used to work and suddenly broke not sure what changed. It broke on pycharm 2.7.3 i upgraded to 3.0 but still broken. The interpeted is added but it throws this error. Any information would be useful. I think it might broke after installing virtual enviroments(not sure)

The paths are ok but the packages are empty and the install button is grayed out. Usualy i install through terminal but it would be nice to get the ide fully working anyway.

Update

which python ->/usr/bin/python

Update 2
I found that some egg files have different permissions.
When i change the permissions to rw r r (644) they come back to 600 for some reason.

umask -> 0022

drwxr-xr-x  2 root root 4096 02.09.2013 01:06 ./
drwxr-xr-x 53 root root 4096 23.09.2013 21:29 ../
-rw-r--r--  1 root root    8 02.09.2013 01:06 top_level.txt
-rw-r--r--  1 root root 1319 02.09.2013 01:06 PKG-INFO
-rw-r--r--  1 root root    1 02.09.2013 01:06 dependency_links.txt
-rw-r--r--  1 root root 5792 02.09.2013 01:06 SOURCES.txt
-rw-r--r--  1 root root 8666 02.09.2013 01:06 installed-files.txt

drwxr-xr-x 4 root root 4096 31.08.2013 22:28 ../
drwxr-xr-x 2 root root 4096 31.08.2013 22:28 ./
-rw------- 1 root root    9 31.08.2013 22:28 top_level.txt
-rw------- 1 root root  563 31.08.2013 22:28 SOURCES.txt
-rw------- 1 root root    3 31.08.2013 22:28 requires.txt
-rw------- 1 root root  425 31.08.2013 22:28 PKG-INFO
-rw------- 1 root root    1 31.08.2013 22:28 not-zip-safe
-rw------- 1 root root    1 31.08.2013 22:28 dependency_links.txt


Traceback (most recent call last): 
File "/usr/lib/python3.3/site-packages/pkg_resources.py", 
line 2304, in _dep_map return self.__dep_map File "/usr/lib/python3.3/site-packages/pkg_resources.py",
line 2374, in __getattr__ raise AttributeError(attr) AttributeError: 
_Distribution__dep_map During handling of the above exception, another exception occurred: 
Traceback (most recent call last): 
File "/home/foobar/Programming/pycharm3/helpers/packaging_tool.py",
line 115, in main do_list() File "/home/foobar/Programming/pycharm3/helpers/packaging_tool.py",
line 47, in do_list requires = ':'.join([str(x) for x in pkg.requires()]) File "/usr/lib/python3.3/site-packages/pkg_resources.py",
line 2323, in requires dm = self._dep_map File "/usr/lib/python3.3/site-packages/pkg_resources.py", 
line 2308, in _dep_map for extra,reqs in split_sections(self._get_metadata(name)): 
File "/usr/lib/python3.3/site-packages/pkg_resources.py", 
line 2750, in split_sections for line in yield_lines(s): 
File "/usr/lib/python3.3/site-packages/pkg_resources.py", 
line 2009, in yield_lines for ss in strs: File "/usr/lib/python3.3/site-packages/pkg_resources.py", 
line 2337, in _get_metadata for line in self.get_metadata_lines(name): 
File "/usr/lib/python3.3/site-packages/pkg_resources.py", 
line 1325, in get_metadata_lines return yield_lines(self.get_metadata(name)) 
File "/usr/lib/python3.3/site-packages/pkg_resources.py", 
line 1322, in get_metadata return self._get(self._fn(self.egg_info,name)).decode("utf-8") 
File "/usr/lib/python3.3/site-packages/pkg_resources.py", 
line 1426, in _get stream = open(path, 'rb') PermissionError: 
[Errno 13] Permission denied: '/usr/lib/python3.3/site-packages/python_dateutil-2.1-py3.3.egg/EGG-INFO/requires.txt'
bpython 0.12 /usr/lib/python3.3/site-packages/bpython-0.12-py3.3.egg 
pygments Pygments 1.6 /usr/lib/python3.3/site-packages/Pygments-1.6-py3.3.egg 
OpenGLContext 2.2.0a2 /usr/lib/python3.3/site-packages/OpenGLContext-2.2.0a2-py3.3.egg 
Cython 0.19.1 /usr/lib/python3.3/site-packages/Cython-0.19.1-py3.3-linux-x86_64.egg 
docutils 0.11 /usr/lib/python3.3/site-packages/docutils-0.11-py3.3.egg 
selenium 2.35.0 /usr/lib/python3.3/site-packages/selenium-2.35.0-py3.3.egg 
numpy 1.7.1 /usr/lib/python3.3/site-packages/numpy-1.7.1-py3.3-linux-x86_64.egg 
matplotlib 1.3.0 /usr/lib/python3.3/site-packages/matplotlib-1.3.0-py3.3-linux-x86_64.egg 
python-dateutil:tornado:pyparsing>=1.5.6:nose nose 1.3.0 /usr/lib/python3.3/site-packages/nose-1.3.0-py3.3.egg 
pyparsing 2.0.1 /usr/lib/python3.3/site-packages/pyparsing-2.0.1-py3.3.egg tornado 3.1 
/usr/lib/python3.3/site-packages/tornado-3.1-py3.3.egg or create new VirtualEnv

Table of Contents
Hide
  1. What is PermissionError: [Errno 13] Permission denied error?
  2. How to Fix PermissionError: [Errno 13] Permission denied error?
    1. Case 1: Insufficient privileges on the file or for Python
    2. Case 2: Providing the file path
    3. Case 3: Ensure file is Closed
  3. Conclusion

If we provide a folder path instead of a file path while reading file or if Python does not have the required permission to perform file operations(open, read, write), you will encounter PermissionError: [Errno 13] Permission denied error

In this article, we will look at what PermissionError: [Errno 13] Permission denied error means and how to resolve this error with examples.

We get this error mainly while performing file operations such as read, write, rename files etc. 

There are three main reasons behind the permission denied error. 

  1. Insufficient privileges on the file or for Python
  2. Passing a folder instead of file
  3. File is already open by other process

How to Fix PermissionError: [Errno 13] Permission denied error?

Let us try to reproduce the “errno 13 permission denied” with the above scenarios and see how to fix them with examples.

Case 1: Insufficient privileges on the file or for Python

Let’s say you have a local CSV file, and it has sensitive information which needs to be protected. You can modify the file permission and ensure that it will be readable only by you.

Now let’s create a Python program to read the file and print its content. 

# Program to read the entire file (absolute path) using read() function
file = open("python.txt", "r")
content = file.read()
print(content)
file.close()

Output

Traceback (most recent call last):
  File "C:/Projects/Tryouts/python.txt", line 2, in <module>
    file = open("python.txt", "r")
PermissionError: [Errno 13] Permission denied: 'python.txt'

When we run the code, we have got  PermissionError: [Errno 13] Permission denied error because the root user creates the file. We are not executing the script in an elevated mode(admin/root).

In windows, we can fix this error by opening the command prompt in administrator mode and executing the Python script to fix the error. The same fix even applies if you are getting “permissionerror winerror 5 access is denied” error

In the case of Linux the issue we can use the sudo command to run the script as a root user.

Alternatively, you can also check the file permission by running the following command.

ls -la

# output
-rw-rw-rw-  1 root  srinivas  46 Jan  29 03:42 python.txt

In the above example, the root user owns the file, and we don’t run Python as a root user, so Python cannot read the file.

We can fix the issue by changing the permission either to a particular user or everyone. Let’s make the file readable and executable by everyone by executing the following command.

chmod 755 python.txt

We can also give permission to specific users instead of making it readable to everyone. We can do this by running the following command.

chown srinivas:admin python.txt

When we run our code back after setting the right permissions, you will get the following output.

Dear User,

Welcome to Python Tutorial

Have a great learning !!!

Cheers

Case 2: Providing the file path

In the below example, we have given a folder path instead of a valid file path, and the Python interpreter will raise errno 13 permission denied error.

# Program to read the entire file (absolute path) using read() function
file = open("C:\Projects\Python\Docs", "r")
content = file.read()
print(content)
file.close()

Output

Traceback (most recent call last):
  File "c:PersonalIJSCodeprogram.py", line 2, in <module>
    file = open("C:\Projects\Python\Docs", "r")
PermissionError: [Errno 13] Permission denied: 'C:\Projects\Python\Docs'

We can fix the error by providing the valid file path, and in case we accept the file path dynamically, we can change our code to ensure if the given file path is a valid file and then process it.

# Program to read the entire file (absolute path) using read() function
file = open("C:\Projects\Python\Docspython.txt", "r")
content = file.read()
print(content)
file.close()

Output

Dear User,

Welcome to Python Tutorial

Have a great learning !!!

Cheers

Case 3: Ensure file is Closed

While performing file operations in Python, we forget to close the file, and it remains in open mode.

Next time, when we access the file, we will get permission denied error as it’s already in use by the other process, and we did not close the file.

We can fix this error by ensuring by closing a file after performing an i/o operation on the file. You can read the following articles to find out how to read files in Python and how to write files in Python.

Conclusion

In Python, If we provide a folder path instead of a file path while reading a file or if the Python does not have the required permission to perform file operations(open, read, write), you will encounter PermissionError: [Errno 13] Permission denied error.

Ezoic

We can solve this error by Providing the right permissions to the file using chown or chmod commands and also ensuring Python is running in the elevated mode permission.

Avatar Of Srinivas Ramakrishna

Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.

The Python error “PermissionError: [Errno 13] Permission denied” happens when you try to do an operation on a file that you don’t have the file permission for. For example, if you need to write to a file, make sure both the file and directory it’s in are writable for you. If you need to read a file, make sure you have permission to read it.

One of the great things about Python is how easy file operations are. With great built-in functions like open() and the with operator, you can open a file, do some stuff to it and/or read it, then de-indent from that block and carry on without having to worry about closing it. Pretty great huh?

But, like all things in life, sometimes it just doesn’t work. One thing that can definitely give new coders grief is file permissions. File permissions will stop you dead in your tracks eventually if you don’t understand them, so it’s best to get a grasp on them now!

Let’s look at the first and most common cause of the dreaded “Permission denied”.

❌ Problem: you tried to create a file in a directory you don’t have write permission for

This is a very common cause that I see new coders run into all the time: they’re trying to write somewhere they have no business writing. Where might that be, exactly? Well, if you’re on a Linux system like me, the / or “root” directory is the usual suspect. Sometimes this is just caused by a stray forward-slash at the beginning of you file name. Sometimes, though, the programmer actually thought he could write there. Which is false.

Here’s an example of just that:

with open("/test.txt", "w") as f:
    f.write("Sup")

Here, we’re trying to open a file called /test.txt. See the problem? The location where we’re trying to open/create it is in the / directory, which is the root directory of your system. That’s not writable by lowly users like us. You need root permissions for that!

Below is what happens when you try to do this:

Traceback (most recent call last):
  File "/home/user/main.py", line 1, in <module>
    with open("/test.txt", "w") as f:
PermissionError: [Errno 13] Permission denied: '/test.txt'

Whoops! Well, you already knew that was coming. Let’s look at how to fix it.

✅ Fix 1: write to a directory you have permission for

The first and best solution to this is to not write to the root directory, or any directory that you don’t have permissions for. This is the recommended solution because, well, if you didn’t have permissions before (and you didn’t accidentally revoke your own permissions on accident), you probably shouldn’t be writing there for good reason.

Below is an example of writing to the current directory instead which, I assume, you have write permissions for because you wrote the Python code there to begin with (duh):

with open("test.txt", "w") as f:
    f.write("Sup")

✅ Fix 2: give yourself write permission for the directory (if possible)

This one might always be possible. But, if for some reason you took way your own permissions, or if someone else did by mistake, you can always try adding them back for yourself.

Let’s say for example you have this in your current directory:

total 8.0K
-rw-rw-r-- 1 user user   60 May 31 20:34 main.py
dr-xr-xr-x 2 user user 4.0K May 31 20:33 testdir/

Here, main.py is our script (source below) and testdir/ is the directory we’re trying to write to. Notice anything wrong? Right off the bat, we can see we do not have write permissions to this directory. In fact, nobody does (except root).

Below is the contents of main.py:

with open("testdir/test.txt", "w") as f:
    f.write("Sup")

Again, we’re just trying to open a file in write mode, which may or may not exist. And we’re trying to do so in testdir, which we don’t have permission for.

To fix this, we need to add write permission to testdir so we can write to it:

If you’re the owner of the directory, the above command should work fine, and you should see the following when you ls -l your working directory:

total 8.0K
-rw-rw-r-- 1 user user   60 May 31 20:34 main.py
drwxrwxr-x 2 user user 4.0K May 31 20:33 testdir/

Voila, now you have write permission for testdir and running your Python script should work just fine.

❌ Problem: you tried to write to a file you don’t have write permission for

If it’s not a directory permission error, it might be an issue with the permissions on the file you’re trying to write to itself. This is less common, but it happens nonetheless.

Let’s take the below directory listing for example:

-rw-rw-r-- 1 user user 52 May 31 20:38 main.py
-r--r--r-- 1 user user  4 May 31 20:39 test.txt

Here, we have a file already in the directory called test.txt which, as you can see, you don’t have write permissions for, it’s read-only.

Below is the contents of main.py, our Python script that we’ll be trying to run:

with open("test.txt", "a") as f:
    f.write("Sup")

Here, unlike the past couple examples, we’re trying to open the file in append mode. This isn’t going to work, because we’re not allowed to write to the file, which append mode needs to be able to do.

If you try to run this code, the following error will occur:

Traceback (most recent call last):
  File "/home/user/main.py", line 1, in <module>
    with open("test.txt", "a") as f:
PermissionError: [Errno 13] Permission denied: 'test.txt'

D’oh! We can fix this, don’t worry. We own the file so we can just give ourselves write permission.

✅ Fix: add write permissions for yourself to the file (if possible)

Like I just mentioned, we own the file, so we can do whatever we want with it. All we have to do to give ourselves write permission here is the following shell command:

Once you run this, run ls -l and you should see the following:

-rw-rw-r-- 1 user user 52 May 31 20:38 main.py
-rw-rw-r-- 1 user user  4 May 31 20:39 test.txt

Nice! Now you have write permission, and your Python script should succeed.

❌ Problem: you tried to read a file you don’t have read permission for

Sometimes, the Python error “PermissionError: [Errno 13] Permission denied” isn’t caused by missing write permissions, though it usually is. No, sometimes it’s caused by missing read permissions. Those are just as important, and you have to have them if you want to open any file for reading.

Take the below directory for example:

total 8.0K
-rw-rw-r-- 1 user user 53 May 31 20:45 main.py
--w--w---- 1 user user  4 May 31 20:44 test.txt

Here, we already have a file called test.txt, but you’ll notice we don’t have read permission for it, only write. Write-only files are weird and rare, but they do exist. Usually by accident.

Here’s the contents of main.py:

with open("test.txt", "r") as f:
    print(f.read())

As you can see we are trying to open this file that we don’t have read permission for for, well, reading. And it’s not going to work. Here’s what happens:

Traceback (most recent call last):
  File "/home/user/main.py", line 1, in <module>
    with open("test.txt", "r") as f:
PermissionError: [Errno 13] Permission denied: 'test.txt'

Yep, saw that one coming didn’t you?

✅ Fix: add read permission for yourself to the directory (if possible)

Since we’re the owner of this file, we can just go ahead and give ourselves read permission with the following shell command:

This should succeed, then you should be able to run your script with no issues.

⚠️ Dangerous Fix: run the script as root (or Administrator on Windows)

If you’re going to do this, you’d better know what you’re doing. Chances are, if you’re getting “PermissionError: [Errno 13] Permission denied” it’s for a good reason, and it’s probably a logic error in your code. It might even be a typo.

If you’re absolutely sure you need to be able to do this, and you have sudo privileges on your Linux or Mac account (or Administrator access on Windows), you can do the following (on Mac or Linux):

sudo python yourscript.py

Just replace yourscript.py with whatever your script is called.

On Windows, you’ll have to open the command prompt as administrator to do this. This is usually done by right-clicking on the command prompt program or shortcut and clicking “run as Administrator”. Then just run your script as normal.

You’ve been warned: this is potentially dangerous! Don’t do it unless you really know what you’re doing!

Conclusion

In this article, we covered what causes “PermissionError: [Errno 13] Permission denied” and how to fix it. It’s always caused by trying to do something that you don’t have permission for. What we focused on here are file permissions, which are the most common. Make sure any file you’re trying to read or write to has the required permissions to be able to do so (for your user account).

That’s all for now, hope it helps!

I am using PyCharm for executing my Python programs. Today, I had tried updating all the packages using Project Interpreter. I received the following error in the process:

error: [Errno 13] Permission denied

enter image description here

After which none of my Python libraries are shown in Project Interpreter list.

I am using PyCharm Community Edition 2016.2.3 in Mac OS X 10.11.6.

bad_coder's user avatar

bad_coder

10.3k20 gold badges43 silver badges65 bronze badges

asked Sep 23, 2016 at 10:46

Sundaravelpandian Singaravel's user avatar

It looks like you need to give your interpretter root permissions. There is a tutorial here on how to do this

answered Sep 23, 2016 at 10:51

user3684792's user avatar

1

Try to remove the shebang if you have it in your code and then try to run it……BTW this Worked for me.

answered Apr 14, 2022 at 17:28

Pranav Nagvekar's user avatar

1

The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the necessary permissions. In your case the file (or directory ???) of interest is /var/folders/2k/_1tccbln53165lgvzvzt3b480000gp/T/tmprqbtrpspycharm-management/setuptools-18.1. You might want to use chmod or chown to change access permissions to it.

answered Jun 22, 2021 at 18:02

Antricks's user avatar

AntricksAntricks

1611 silver badge9 bronze badges

  1. sudo visudo -f /etc/sudoers.d/python

  2. inside the /etc/sudoers.d/python you just created enter these details in the given format <current_username> <hostname> = (root) NOPASSWD: <full path to python> ie mostly you might have creating a virtual env in pycharm when you start a project. so in that you would find a bin folder. inside which you will find many python virtual interpreters(mention that absolute path ex /home/<project_foldername>/bin/python).

  3. anywhere you can create shell script python-sudo.sh

  4. inside python-sudo.sh mention like this #!/bin/bash sudo <same_path_you_given_in_"/etc/sudoers.d/python"_file> "$@"

  5. Now open your pycharm and project you need to run/debugg the go to setting > project:projectname > Python Interpreter Click on Wheel Icon and Then click on «Add»

  6. In the new window that popup select Existing Environment > Select the shell script which you created python-sudo.sh. Then click OK > and then Click Apply

  7. Make sure that on top right side of editor near to green run button Run/Debug configuration small drop down menu for your file which you are about to execute/debug click on Edit Configuration and the same python interpreter is selected there as well which is python-sudo.sh.

  8. Now try executing/debugging, it should work.

Tomerikoo's user avatar

Tomerikoo

17.6k16 gold badges39 silver badges58 bronze badges

answered Jul 21, 2021 at 12:11

hirandas2610's user avatar

I had the same issue after changing python directory. Then I changed the interpreter setting to a new python directory then ‘Run’ the code from the Menu and selected 2nd ‘Run’ option or you can just use the shortcut as Alt+Shift+F10′ and a small window appears with your code file name then select the filename which you cant to Run.

This resolved my issue!

enter image description here

Sach's user avatar

Sach

8948 silver badges20 bronze badges

answered Jun 23, 2022 at 13:53

Akash Shahapure's user avatar

I encountered the same problem. When I removed the python libs installed out of Pycharm IDE, I found that there is no installation problem. Please use the pip uninstall command.

answered Jul 26, 2019 at 13:17

salee's user avatar

saleesalee

1891 gold badge1 silver badge9 bronze badges

1

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Permission denied publickey ssh ошибка windows
  • Perl exe ошибка приложения