7

So python installed probably fine. Or perhaps it was already on here (I'm using xubuntu 12.10).

But I tried to install networkx today using pip:

pip install networkx

then says I don't have permission. So I sudo the above command. Installs without errors.

Now I can't import networkx without being root. I'm fairly confident you shouldn't have to be root all the time to run python scripts. That sounds really dangerous.

easy_install says this:

easy_install networkx

error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the installation directory:

[Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/test-easy-install-13206.pth'

The installation directory you specified (via --install-dir, --prefix, or the distutils default setting) was:

/usr/local/lib/python2.7/dist-packages/

Perhaps your account does not have write access to this directory? If the installation directory is a system-owned directory, you may need to sign in as the administrator or "root" account. If you do not have administrative access to this machine, you may wish to choose a different installation directory, preferably one that is listed in your PYTHONPATH environment variable.

For information on other options, you may wish to consult the documentation at:

http://packages.python.org/distribute/easy_install.html

Please make the appropriate changes for your system and try again.

I also tried installing into ~/.networkx (a subfolder i created as not-root) and I get the same permissions error. I chmod 777 /usr/local/lib/python2.7/dist-packages and try to install, same permissions error.

pip uninstall and sudo easy_install causes the same problems as the pip install.

which by the way is:

python t1.py

Traceback (most recent call last): File "t1.py", line 3, in import networkx as nx ImportError: No module named networkx

sudo python t1.py

H: 10 ... more stuff that indicate its importing and working fine ...

Clearly, I've just been a noob at some point in either installing python, or... anywhere else. I don't know where, it could be anywhere. Has anyone encountered this before or is cluey enough to know what's going on? I need your halp. Cheers.

EDIT: (More info for Radoo)

sean@potatocake:~$./sh.sh
User is not part of the group which has access to that directory.
/usr/local/lib/python2.7/dist-packages needs access.

sean@potatocake:~$ll /usr/local/lib/
total 12
drwxrwxr-x 3 root root  4096 Mar 28 19:00 perl
drwxrwsr-x 4 root staff 4096 Oct 18 04:07 python2.7
drwxrwsr-x 3 root staff 4096 Oct 18 04:05 python3.2

sean@potatocake:~$ll /usr/local/lib/python2.7/
total 8
drwxrwsr-x 32 root staff 4096 Mar 28 23:13 dist-packages
drwxrwsr-x  2 root staff 4096 Oct 18 04:07 site-packages

note: I chmod'd this back to 775 when a non-sudo pip still didn't work.

sean@potatocake:~$groups
sean adm cdrom sudo dip plugdev lpadmin sambashare

sean@potatocake:~$sudo useradd -G staff sean
useradd: user 'sean' already exists
5
  • What error do you get when you try to import?
    – terdon
    Mar 29, 2013 at 12:18
  • python t1.py Traceback (most recent call last): File "t1.py", line 3, in import networkx as nx ImportError: No module named networkx
    – ninjamario
    Mar 29, 2013 at 12:40
  • Ugh sorry, can't make that pretty. If you look above in the original post it is there nicely under the results for 'python t1.py' - cheers
    – ninjamario
    Mar 29, 2013 at 12:41
  • Are you certain you're using the right python? What does type python return? Nov 10, 2015 at 8:36
  • Can you try - pip install --user <pkg-name> pkg-name = networkx
    – Deepak
    Sep 12, 2018 at 8:52

3 Answers 3

1

It's best not to try to override the system's version of Python. That version is there for the system. Customization to system's Python might cause conflicts or even open your system up to vulnerabilities. Also, system updates will probably revert your customization.

It's best to use your own version of Python, which can be done with tools such as virtualenv or pyenv.

0

I'm definitely not an expert on this, but here are my thoughts.

It's good to have your install as root, so nobody can modify your files and configurations, but root and sudoers. So I guess your installation is OK. If you want to install a new package you should be root.

As for the permission error, you said you did this: chmod 777 /usr/local/lib/python2.7/dist-packages. Well, this offers access just to that directory at the end. You might have some parent directories which may not have access permissions (execute rights), which can cause your issue.

To check permissions for the current user on all those directories, you'll have to do something like this (I guess you can find a better version but anyway) for each of them:

dir="<directory_path>"
user_groups=$(groups <user>)
dir_group=$(stat "$dir" | cut -d' ' -f6)

if ! [[ "$dir_group" =~ "\\b$user_groups\\b" ]]; then printf "User is not part of the group which has access to that directory.\n"; fi

dir_access=$(stat "$dir" | cut -d' ' -f3)
group_dir_exec_access=${dir_access:6:1}

if [ "$group_dir_exec_access" != "x" ]; then printf "This dir needs access.\n"; <add some sudo chmod stuff to give access to group> fi

I hope this helps.

3
  • Thanks for the quick reply, sorry it took me a while to get back to you (sleep, then a full day =P). I pasted a bunch of (hopefully) relevant command outputs in an edit section up top. Your script is sh.sh btw, in case you can't recognise your own printf contents ;)
    – ninjamario
    Mar 29, 2013 at 12:17
  • Oops, just meant to newline, not submit comment. So if I want to install a new package I should be root? In that case the permissions thing should be 775 right. Or possibly even less. 755. Regardless, I can install the package fine if I sudo it but then I can't import the package unless python is running as root - how can I make networkx available to my normal user (sean)?
    – ninjamario
    Mar 29, 2013 at 12:20
  • I didn't get your printf comment, but anyway, the networkx file should have read and execute rights I guess for the group, and sean should be in that group.
    – user127350
    Mar 29, 2013 at 15:47
-1

I faced the same problem while installing Buildozer. I executed following commands and it worked for me. Try:

  1. Change directory to Python2.7
  2. sudo chmod 777 dist-packages
  3. sudo chmod 777 etc
  4. and then python2.7 setup.py install

It worked for me!

2
  • 3
    -1 Blind use of chmod 777 (= drwxrwxrwx) is a very bad idea, specially on above mentioned directories, as that would give any user write permission (last w in drwxrwxrwx) for them. Even group write (second w in drwxrwxrwx) should not be given. Its better to just give read and execute permission on dist-packages by chmod o+r,o+X dist-packages.
    – Sithsu
    Jul 24, 2015 at 14:39
  • If you have to use sudo go directly with sudo -H pip install <whatever> and get over with it. This doesn't make sense. As @Sithsu point's out, having 777 permissions on anything is a bad idea anyway.
    – atmosx
    Dec 25, 2015 at 21:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .