0

When running pip install django-cron I get the following error:

ERROR: Error [Errno 13] Permission denied: '/vagrant/.venv/bin/python' while executing command python setup.py egg_info
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/vagrant/.venv/bin/python'
Consider using the `--user` option or check the permissions.

However, if I use --user, I get a different error saying:

ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

My venv is activated.

When I previously tried installing libraries, everything worked, if I use the sudo command I get the following warning:

WARNING: The directory '/home/vagrant/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. 

Using -H doesn't resolve the issue sadly, I am not sure how I can change my access to the .venv file, any help would be appreciated.

I only get this error for Python modules django-cron and django-crontab, but other modules like pillow can be installed successfully.

Edit 4: My setup is a bit janky, as I am using Vagrant, but I have PyCharm Community Editon, so I end up downloading the packages twice, once just so the editor would recognize it and another time for Vagrant where I run the program, and when I did this in PyCharm, it worked in PyCharm.

This is the Vagrantfile I used:

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-18.04"

  config.vm.network "forwarded_port", guest: 8080, host: 8080

  config.vm.provision "shell", inline: <<-SHELL
    sudo apt-get install python3-distutils -y
    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    sudo python3 get-pip.py
    rm get-pip.py
    sudo pip install virtualenv
    cd /vagrant 
    virtualenv -p /usr/bin/python3 .venv --always-copy
    echo "cd /vagrant && source /vagrant/.venv/bin/activate" >> /home/vagrant/.profile
  SHELL
end
wovano
  • 4,543
  • 5
  • 22
  • 49
MareksNo
  • 140
  • 2
  • 12
  • 1
    clearly it is a permission issue with writing files to that folder. But why are you using venv in vagrant where it already provides the application isolation from your OS? – ruddra Aug 17 '20 at 08:51
  • 1
    Well, does your user have write permission to ``/vagrant/.venv/bin/python`` or any parent directories? – MisterMiyagi Aug 17 '20 at 08:55
  • MisterMiygagi, not sure, but when I previpusly installed packages, I didnt have such error. – MareksNo Aug 17 '20 at 09:17
  • Ruddra, what do you mean by that? – MareksNo Aug 17 '20 at 09:20
  • Do you mean like: "sudo pip install – MareksNo Aug 17 '20 at 09:29
  • Since you are using Vagrant, can you add your Vagrant file (or at least the relevant part of it) to your question? – wovano Aug 24 '20 at 07:56
  • Thanks for adding the Vagrantfile. This confirms what I expected. See my answer :-) – wovano Aug 24 '20 at 10:36
  • Okay, I just tested this Vagrantfile and it works fine for me. I can SSH to the VM and run `pip install numpy` (just as a test). This works fine. So you'll need to specify in the question: how did you create the VM? Did you get any errors? How do you connect to the VM? What is the exact command that you typed to get the above error? Where did you type this command? And what operating system are you using? Adding this information might help us understand and possibly reproduce the problem. – wovano Aug 24 '20 at 15:05
  • I typed the exact command and even created a new vm, try installing django-cron, and what main system do you have what os? – MareksNo Aug 24 '20 at 15:07
  • I tried installing pillow and other django packages and they work fine but when I try to isntall django-cron and django-crontab this happens, the exact command is any pip install command from just pip install package to sudo -H pip install package – MareksNo Aug 24 '20 at 15:08
  • Ah! The command `pip install django-cron` or `pip install django-crontab` is never mentioned in your question... And that is important if that is the command that causes the error!! This fails in my Vagrant VM too. Not sure why... Might be a bug in the django-cron module. – wovano Aug 24 '20 at 16:21

3 Answers3

2

By default, Vagrant provisioning scripts are executed as root. Since you create the virtual environment during the provisioning the directories are owned by root and not accessible for the normal user (vagrant).

To solve this, you should set the shell provisioning option "privileged" to false.

Change this line:

config.vm.provision "shell", inline: <<-SHELL

to:

config.vm.provision "shell", privileged: false, inline: <<-SHELL

Alternatively, you could modify your provisioning script to run the virtualenv command as the vagrant user using the following command:

sudo -u vagrant virtualenv -p /usr/bin/python3 .venv --always-copy

UPDATE:

Although the above is generally true, it's not the cause of the problem in your case, since you installed the virtual environment inside /vagrant, which is a virtual mount of the directory on your host machine (the directory where your Vagrantfile is stored). Normal file permissions do not apply, or at least not in the usual way, for this directory.

It seems that the Python modules django-cron and django-crontab have an issue with this mount, for whatever reason (might be a bug).

Creating the virtual environment inside the VM file system instead of the host file system solves the problem. You could use the following Vagrantfile. I tested this and I could install django-cron without errors.

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-18.04"

  config.vm.network "forwarded_port", guest: 8080, host: 8080

  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    sudo apt-get install python3-distutils -y
    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    sudo python3 get-pip.py
    rm get-pip.py
    sudo pip install virtualenv
    virtualenv -p /usr/bin/python3 /home/vagrant/venv --always-copy
    echo "cd /vagrant && source /home/vagrant/venv/bin/activate" >> /home/vagrant/.profile
  SHELL
end
wovano
  • 4,543
  • 5
  • 22
  • 49
  • Alright thank you, if this works you are the best, I already had given up hope almost, can I just edit the vagarntfile btw? – MareksNo Aug 24 '20 at 11:47
  • 1
    @MareksNo, I assumed you already know how to use Vagrant. You can just edit the Vagrantfile indeed, but you'd have to recreate the VM in order to fix the problem (`vagrant destroy` followed by `vagrant up`). This destroys all data you stored in the VM and all software you installed on it after you initially created it. If you don't want to recreate the VM a quick fix is running `chown -R vagrant: /vagrant/.venv` inside the VM. But if you don't fix the Vagrantfile, the problem will re-appear the next time you create the VM. – wovano Aug 24 '20 at 13:16
  • I giess I will do chown and then edit the file for next time I use it, I hadnt had to previously change the vagrant file so didnt know how to do that, but thanks. – MareksNo Aug 24 '20 at 14:22
  • So I was trying to use chown, and there were ni errors from that, but still tge same error – MareksNo Aug 24 '20 at 14:35
  • Created a new vm just to test with the new Vagrantfile, and still the same error – MareksNo Aug 24 '20 at 14:42
  • 1
    @MareksNo, I updated your question to clarify the issue and updated my answer to provide a solution. This seems to work! – wovano Aug 24 '20 at 16:46
  • Oh wow it works, thank you so much, but how did you come up with such solution and why does it work as I am confused by it. thanks again – MareksNo Aug 24 '20 at 16:56
  • @MareksNo, I'm not sure why the error occurred, but I guess the `/vagrant` directory is just a bit of a special. It's a non-Linux file system in a Linux environment, and this results in unexpected behavior sometimes. I'm glad this solution worked for you :-) – wovano Aug 24 '20 at 20:14
1

This usually happens when you don't have write access to your /vagrant/.venv folder. You can check access with ls -l cmd.

If so, you should change your access on your /vagrant/.venv folder.

Szymon
  • 19
  • 4
  • `higlighted ` sometimes means soft links. I don't think this is relavant. – Szymon Aug 17 '20 at 09:30
  • Sorry I ran the wrong command, that time, Im not sure I understand what it outputs, should I maybe send the output? – MareksNo Aug 17 '20 at 09:33
  • drwxrwxrwx, This is the what I guess the permission for venv, sorry if I am wrong. -rwxrwxrwx 1 vagrant vagrant 4526456 Jul 12 09:10 /vagrant/.venv/bin/python, Permission for the route – MareksNo Aug 17 '20 at 10:03
  • Also after running whoami, I gor the output saying vagrant, and every file I checked it said rwxrwxrwx, when installing a different package everything worked. – MareksNo Aug 18 '20 at 07:17
  • Sorry for not log in stackoverflow for month. Glad you get this by yourself. – Szymon Jan 06 '22 at 03:37
-1

Just try to use the pip command in cmd

pip install <packagename>