0

I am using a conda environment and some local packages which are installed using pip install -e.

Sometimes I need to run a script as root. When doing so I explicitly use the conda environment, but it does not find my local packages. When using without sudo, everything is working fine.

$ /path/to/env/bin/python -c "import my_module" --> works fine
$ sudo /path/to/env/bin/python -c "import my_module"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'my_module'

I have also tried persisting the environment like this:

$ sudo -E /path/to/env/bin/python -c "import my_module"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'my_module'

Setup:

  • Ubuntu 18.04
  • python 3.7.10
AMC
  • 2,642
  • 7
  • 13
  • 35
ConstantinB
  • 161
  • 9
  • Is the environment activated? – AMC May 05 '21 at 20:03
  • I directly reference the environment when calling Python or pip like `/path/to/env/bin/python|pip` so that should not be am issue, nor would the root's python/pip link to anything unexpected – ConstantinB May 05 '21 at 21:50
  • There are some other things that happen when activating the environment, but I'm not sure if that's part of the issue here. – AMC May 07 '21 at 14:28

1 Answers1

1

Python (or Anaconda in your case) has few different environments on your computer, when installing packages as a user they are probably installed on ~/.local/python/... and when installing as root they are installed on /var/lib/python/....

When you are running python as a user and importing a package it we'll look in several places including the local directory.
But when running python as root it won't look in these places..

The most simple solution is to install these packages using sudo, or start using venv which is highly more recommended.

Mogi
  • 596
  • 1
  • 6
  • 18
  • What would be the advantage of using `venv`? It appears to also require packages to be installed using `sudo` to be able to access them from root, or do I need to setup the venv differently? – ConstantinB May 05 '21 at 12:02
  • you can use `venv` to create a separate environment for each project, and then when you activate it you know everything installed through it is usable when running python and there is nothing you don't need installed in it. You can read more here https://stackoverflow.com/questions/41972261/what-is-a-virtualenv-and-why-should-i-use-one – Mogi May 05 '21 at 17:00
  • @Mogi _you can use venv to create a separate environment for each project_ That would fulfill the same purpose as Conda. – AMC May 05 '21 at 20:02
  • @Mogi I am aware of how to use venv. But it seems to not make any difference in my case when installing local packages compared to using conda. It still requires me to install local packages using root privileges to later access them from root. With conventional packages this does not appear to be an issue as packages installed without sudo are also available when calling Python as root. – ConstantinB May 05 '21 at 21:58
  • No it isn't, when using venv the packages are installed within this env no matter if root or the user wanted to install them.. your problem isn't the privileges you are using is they place pip (or conda) installs them which are by default different between root and the user – Mogi May 06 '21 at 16:47
  • 1
    @ConstantinB [This question](https://stackoverflow.com/q/36659448/11301900) seems like it might be related to what Mogi is talking about. – AMC May 07 '21 at 14:30
  • 1
    [This other question](https://stackoverflow.com/q/30626160/11301900) might also be useful. – AMC May 07 '21 at 14:31