Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8013

Python • Re: error: externally-managed-environment

$
0
0
You can also use a “shebang” line which points to the environment’s Python interpreter,
i.e. #!<path-to-venv>/bin/python (the <path-to-venv> MUST be a fully qualified path).
This is how scripts installed into the virtual environment work.
I am not sure how you mean by that, what scripts would be installed into the virtual environment.

My understanding is that Python programs should use the recommended '#!/usr/bin/env python', can be placed anywhere, and Python will sort out where it imports required modules from based on whatever virtual environment has been activated, based on whatever PATH adjustments are made when the virtual environment is activated.

One should only have to run Python via the virtual environment link when the virtual environment hasn't been activated, such as when run from 'crontab', 'rc.local' etc.

Even then it may be better to login as the user who owns the virtual environment and let that sort out what's actually needed. See the Running A Program At Start Up A Beginner's Guide from thagrol for details on doing that.

I wouldn't recommend putting any fully qualified path to user specific virtual environments in Python program shebangs because that will cause major portability problems, if you have multiple users using the same program, or you want to use it on multiple machines.

For similar reasons I wouldn't recommend placing user programs within the virtual environment itself.

Statistics: Posted by hippy — Mon Jul 07, 2025 11:24 am



Viewing all articles
Browse latest Browse all 8013

Trending Articles