Issue
With a bash script I have created a virtual environment for my project, the bash script is kept in the same directory as the project:
#!/bin/bash
virtualenv=~/Library/Python/3.7/lib/python/site-packages/virtualenv.py
wd=$(dirname "$BASH_SOURCE")
env_dir=~/virtual_environments/invest_scripts
$virtualenv $env_dir
cd -- "$(dirname "$BASH_SOURCE")"
source $env_dir/bin/activate
sudo python3 setup.py develop
deactivate
After running the script, I activate the environment using
source ~/virtual_environments/invest_scripts/bin/activate
But I get the following outputs
(invest_scripts) $which python
(invest_scripts) $/Users/name/virtual_environments/invest_scripts/bin/python
(invest_scripts) $which python3
(invest_scripts) $/usr/bin/python3
Additionally I found that when I open the interactive python3
shell when the virtual environment is not activated, and import a package which was specified in setup.py
. The file points to the egg shown from my project.
$ python3
Python 3.7.3 (default, Nov 15 2019, 04:04:52)
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.__file__
'/Users/name/invest_scripts/.eggs/numpy-1.18.0-py3.7-macosx-10.7 x86_64.egg/numpy/__init__.py'
Could someone explain what is happening?
Solution
sudo
executes commands using a new shell, it won't copy across the PATH
environment variable. This is not problem specific to virtualenv
s. See https://unix.stackexchange.com/questions/83191/how-to-make-sudo-preserve-path for the generic solution to this.
However, you don't need to rely on shell executable resolution. Activating a virtualenv essentially just sets your PATH
variable so that your shell finds $env_dir/bin/python
first when you use the unqualified python
executable. You can just use the full path of the $env_dir/bin/python
executable and so completely sidestep executable lookups:
sudo $env_dir/bin/python setup.py install
However, you generally want to avoid switching users when installing packages into a virtualenv, especially root. There should not be any need to. If you have permission issues in your virtualenv then you have used sudo
in the past with it, and you don't want to compound that issue. In that case repair your virtualenv permissions by recursively re-assigning the files to your own account: sudo chown -R $UID:`id -g` $env_dir
.
Next, rather than use setup.py install
, use pip
to do the installing. Just use the pip
command in the virtualenv point it to the directory containing the setup.py
file. If that's the current directory, use:
$env_dir/bin/pip .
Answered By - Martijn Pieters Answer Checked By - Timothy Miller (WPSolving Admin)