When you install things using pip on the command line, there are a few ways to go about it: 1. install with sudo, in which case the package usually ends up in the system directories so the package will be available for all users on that machine. It is usually a good idea to avoid this. 2. you don’t use sudo, but just “pip install <package-name>” in which case the package will only be available for the one installing the package. Or 3. use venv.
Packages you install for yourself are commonly stored under (for Python 3.10.x):
/home/\<username\>/.local/lib/python3.10/site-packages
To create a virtual environment in Python, you create a directory where you want to work, then open a Terminal window and issue the command, for example:
python3 -m venv tutorial-env
More info here:
The venv manager allows you to use different versions of packages, or different versions of Python, should you have them installed. It is quite a safe playground. Therefore, anything you install inside venv, will only be available inside venv (as far as I know). You have to activate that virtual environment to be able to see and use the packages in Python. Therefore, the system will never use them if you don’t have this environment activated and this is the secure bit. 8)
If you want to be able to run Python and use a package you installed for yourself, without having to activate venv, you can install the package for yourself only. This method seems to be frowned upon, but I have yet to find a problem with it. How that might work can be seen here:
$ sudo python
[sudo] password for qruqs:
Python 3.10.9 (main, Dec 19 2022, 17:35:49) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import Levenshtein
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'Levenshtein'
>>>
And yes, it’s spelled that way.
However, I actually do have the Levenshtein module installed for my own user. The system just cannot find it because it has its own “home” for Python packages and it’s not installed there. Example:
$ python
Python 3.10.9 (main, Dec 19 2022, 17:35:49) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import Levenshtein
>>> dir(Levenshtein)
['__author__', '__builtins__', '__cached__', '__doc__', '__file__',
'__license__', '__loader__', '__name__', '__package__', '__path__',
'__spec__', '__version__', '_levenshtein', '_string_metric', 'apply_edit',
'c_levenshtein', 'distance', 'editops', 'hamming', 'inverse', 'jaro',
'jaro_winkler', 'matching_blocks', 'median', 'median_improve', 'opcodes',
'quickmedian', 'ratio', 'seqratio', 'setmedian', 'setratio',
'subtract_edit']
>>>
It works, no problems.
Another, similar system as venv which is also quite common is conda (aka anaconda). Personally I don’t like it, it is very in-your-face, so to speak, and it also drags in features from iPython, which I like even less. ESRI’s ArcGIS (google that if interested) uses it and I’m hating every minute of it. Again, just a personal opinion. 8)
Google for “python anaconda” and you will probably find it.
Conclusion
If you only have used:
pip install <python-package-name>
You can probably just move those packages someplace else (or create a directory listing of them all, so you know which you had and delete them) and start using venv and install things your need there instead.
Hope this helps.