# Using sudo inside venv

**URL:** <https://discuss.python.org/t/using-sudo-inside-venv/29148>\
**Category:** Python Help\
**Tags:** help\
**Created:** [July 7, 2023, 4:08am UTC](https://discuss.python.org/t/using-sudo-inside-venv/29148 "2023-07-07T04:08:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://avatars.discourse-cdn.com/v4/letter/p/87869e/32.png) [@pauljurczak](https://discuss.python.org/u/pauljurczak)\
**Post date:** [July 7, 2023, 4:08am UTC](https://discuss.python.org/t/using-sudo-inside-venv/29148/1 "2023-07-07T04:08:22Z")

</div>

I’m running a script successfully inside a virtual environment created with `venv`, e.g.

```auto
(.venv) paul@desktop:~/st-python/clock-mpl$ python3 clock-4.py

```

I would like to run this script as a root inside the same virtual environment, but this doesn’t work:

```auto
(.venv) paul@desktop:~/st-python/clock-mpl$ sudo python3 clock-4.py
Traceback (most recent call last):
  File "/home/paul/st-python/clock-mpl/clock-4.py", line 1, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

```

In sudo mode, Python is using different environment. Is there a workaround?

---

<div class="post-metadata">

**Author:** ![defjaf](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/defjaf/32/12838_2.png) [@defjaf](https://discuss.python.org/u/defjaf)\
**Post date:** [July 7, 2023, 5:38am UTC](https://discuss.python.org/t/using-sudo-inside-venv/29148/2 "2023-07-07T05:38:08Z")

</div>

Is `python3` referring to the same executable in both cases? (Try `which python3` and `sudo which python3`) Probably not, since it’s a venv — you’ll need to call that one directly.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [July 7, 2023, 5:44am UTC](https://discuss.python.org/t/using-sudo-inside-venv/29148/3 "2023-07-07T05:44:25Z")

</div>

> [@pauljurczak](#):
>
> In sudo mode, Python is using different environment. Is there a workaround?

Sudo is not running the puthon3 in the venv.

Use the full path to the python3 in the venv.  
This avoids the need to activate the venv.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://avatars.discourse-cdn.com/v4/letter/p/87869e/32.png) [@pauljurczak](https://discuss.python.org/u/pauljurczak)\
**Post date:** [July 7, 2023, 6:04am UTC](https://discuss.python.org/t/using-sudo-inside-venv/29148/4 "2023-07-07T06:04:46Z")

</div>

> Use the full path to the python3 in the venv.

Thank you. It works!

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://avatars.discourse-cdn.com/v4/letter/p/87869e/32.png) [@pauljurczak](https://discuss.python.org/u/pauljurczak)\
**Post date:** [July 7, 2023, 6:07am UTC](https://discuss.python.org/t/using-sudo-inside-venv/29148/5 "2023-07-07T06:07:23Z")

</div>

> [@defjaf](#):
>
> Is `python3` referring to the same executable in both cases?

No, it’s not. Thank you.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [July 7, 2023, 6:08pm UTC](https://discuss.python.org/t/using-sudo-inside-venv/29148/6 "2023-07-07T18:08:07Z")

</div>

Why does this happen? Do commands run “as” root with `sudo` use a separate PATH for the root user, or something like that?

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [July 7, 2023, 6:38pm UTC](https://discuss.python.org/t/using-sudo-inside-venv/29148/7 "2023-07-07T18:38:03Z")

</div>

It depends on your configuration and what command-line options you  
pass to your sudo invocation, but often yes. For example, on modern  
Debian systems this is controlled by default with a secure\_path  
option in the /etc/sudoers file.
