Create Python Virtual Environment
I attempted to install MkDocs on one of my Linux computers. I ran into an issue where it would display the below message.
almostengr@peppermint:~$ python3 -m pip install mkdocs
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.
Not sure what this message meant, I followed the instructions that were provided. Below is what happened.
almostengr@peppermint:~$ sudo apt install python3-mkdocs
[sudo] password for almostengr:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package python3-mkdocs
The above was not surprising, but also slightly irritating. Did what it suggested and it still did not work.
almostengr@peppermint:~$ python3 -m venv python
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.12-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: /home/almostengr/python/bin/python3
almostengr@peppermint:~$ sudo apt install python3-venv
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
python3-pip-whl python3-setuptools-whl python3.12-venv
The following NEW packages will be installed:
python3-pip-whl python3-setuptools-whl python3-venv python3.12-venv
0 upgraded, 4 newly installed, 0 to remove and 1 not upgraded.
Need to get 2,430 kB of archives.
After this operation, 2,783 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Installation completed succesfully.
Tried running the same command again to install MkDocs, but ran
into an error.
Did some research and found that I need to source the activate
file within the bin directory of the virtual environment.
almostengr@peppermint:~/python$ ls
bin include lib lib64 pyvenv.cfg
almostengr@peppermint:~/python$ cd bin/
almostengr@peppermint:~/python/bin$ ls
activate activate.csh activate.fish Activate.ps1 pip pip3 pip3.12 python python3 python3.12
almostengr@peppermint:~/python/bin$ source activate
(python) almostengr@peppermint:~/python/bin$ pip3 install mkdocs
After doing all of that, the command finally works.
(python) almostengr@peppermint:~/python/bin$ mkdocs
Usage: mkdocs [OPTIONS] COMMAND [ARGS]...
MkDocs - Project documentation with Markdown.
Options:
-V, --version Show the version and exit.
-q, --quiet Silence warnings
-v, --verbose Enable verbose output
--color / --no-color Force enable or disable color and wrapping for the output. Default is auto-detect.
-h, --help Show this message and exit.
Commands:
build Build the MkDocs documentation.
get-deps Show required PyPI packages inferred from plugins in mkdocs.yml.
gh-deploy Deploy your documentation to GitHub Pages.
new Create a new MkDocs project.
serve Run the builtin development server.
Now to prevent having to source the Python virtual environment, I updated the .bashrc file to include the below.
alias mkdocs='/home/almostengr/python/bin/mkdocs'
This will call the mkdocs files that have been loaded onto the computer each time.