Solved How to properly install and use python modules in FreeBSD?

Hello,

I'm a newbie in both FreeBSD and programming. I'm now taking CS50 and trying to write code in VSCode.

I would like to know how to properly install python modules. pip install <python-module> or doas pkg install py38-<python-module>

And in case if there is no pkg version, what should I do?

And in general, how to properly use python in FreeBSD?

Thanks.
 
You install python38, py38-pip, the py38 modules which do exist. If none exist use pip eg. "pip install --user pip-install-test".
If that fails, you can start developing a module for freebsd.
 
You install python38, py38-pip, the py38 modules which do exist. If none exist use pip eg. "pip install --user pip-install-test".
If that fails, you can start developing a module for freebsd.
Is there any chance of messing the system by using pip? If yes, is there any good practice to prevent it?
 
Yes there is a good way using pip.
1. Always use pip as regular user, never as root
2. Always give the parameter "--user" when installing. So pip will stay out of your system directory.
Pip always installs in "~/.local/lib/python3.8/site-packages". It is safe to delete that directory if needed.
 
Hello,

I'm a newbie in both FreeBSD and programming. I'm now taking CS50 and trying to write code in VSCode.

I would like to know how to properly install python modules. pip install <python-module> or doas pkg install py38-<python-module>

And in case if there is no pkg version, what should I do?

And in general, how to properly use python in FreeBSD?

Thanks.
You should always use the package system (either ports or packages), if there's a module missing create a port and possibly submit if you'll maintain the port afterwards.

 
Is there any chance of messing the system by using pip? If yes, is there any good practice to prevent it?

I haven't done this on FreeBSD yet, but I've had pretty good luck with Python's virtual environments using virtualenvwrapper on Ubuntu, and it looks like you can even select the version of Python to use on a per-project basis, although I have only used Python 3 so far.

Installation and use looks something like this. Read the docs because these are just my notes from last time I did it.

Code:
$ pip -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
$ pip install --user virtualenvwrapper
[...]
$ tail .profile
# run virtualenvwrapper.s 
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
export VIRTUALENVWRAPPER_VIRTUALENV=$(which virtualenv)
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
export WORKON_HOME=$HOME/Envs
export PROJECT_HOME=$HOME/projects
. "$(which virtualenvwrapper.sh)"
$ env | grep 'PROJECT\|VIRTUAL\|WORK'
VIRTUALENVWRAPPER_VIRTUALENV=/home/ccammack/.local/bin/virtualenv
VIRTUALENVWRAPPER_SCRIPT=/home/ccammack/.local/bin/virtualenvwrapper.sh
VIRTUALENVWRAPPER_WORKON_CD=1
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
WORKON_HOME=/home/ccammack/Envs
VIRTUALENVWRAPPER_PROJECT_FILENAME=.project
VIRTUALENVWRAPPER_HOOK_DIR=/home/ccammack/Envs
PROJECT_HOME=/home/ccammack/projects
$ mkvirtualenv --version
virtualenv 20.10.0 from /home/ccammack/.local/lib/python3.8/site-packages/virtualenv/__init__.py
$ workon
$ mkvirtualenv temp
(temp) $ workon
temp
(temp) $ echo $VIRTUAL_ENV
/home/ccammack/Envs/temp
(temp) $ lssitepackages
(temp) $ pip install ...
[...]
$ cpvirtualenv temp temp2
$ rmvirtualenv temp
$ deactivate temp2
$ rmvirtualenv temp2
$ mkproject temp3

Exporting the environment for use by someone else looks something like this:

Code:
(temp) $ pip freeze > requirements.txt
(temp) $ cat requirements.txt
certifi==2021.5.30
charset-normalizer==2.0.3
future==0.18.2
idna==3.2
lxml==4.6.3
requests==2.26.0
setproctitle==1.2.2
SimplePool==0.1
urllib3==1.26.6
...
(different-env) $ pip install -r requirements.txt
 
Top