Starting with Ubuntu 24, my operating system forces me to install Python "externally managed environments" into a virtual environment, and issues an error message when I try without:
At the Linux shell, the process to create and activate the virtual environment is as follows:
Afterwards, any -pip install- installs packages to the virtual environment, and the packages can be accessed without further ado:
In Stata, however, the /activated/ virtual environment is not detected:
Consequently packages installed to the virtual environment are not detected:
Also, setting -python set userpath- does not help:
This is
Is there any way to install Python packages in a virtual environment such that Stata can access them?
Many regards
Uli
Code:
pip install beautifulsoup4 error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. If you wish to install a non-Debian packaged Python application, it may be easiest to use pipx install xyz, which will manage a virtual environment for you. Make sure you have pipx installed. See /usr/share/doc/python3.12/README.venv for more information. note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification.
Code:
$ python3 -m venv mypython_env $ source mypython_env/bin/activate
Code:
$ pip install beautifulsoup4 $ pip show beautifulsoup4 [...] Location: /home/ukohler/mypython_env/lib/python3.12/site-packages [...]
Code:
. python search ------------------------------------------------------------------------------- Python environments found: /usr/bin/python3 /usr/bin/python3.6 /usr/bin/python3.6m -------------------------------------------------------------------------------
Code:
. python which beautifulsoup4 Python module beautifulsoup4 not found r(601);
Code:
. python set userpath /home/ukohler/mypython_env/lib/python3.12/site-packages . python query ------------------------------------------------------------------------------- Python Settings set python_exec /usr/bin/python3 set python_userpath /home/ukohler/mypython_env/lib/python3.12/site-packages Python system information [..] . python which beautifulsoup4 Python module beautifulsoup4 not found r(601);
Code:
. version version 19.5
Many regards
Uli
Comment