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.
Is there any chance of messing the system by using pip? If yes, is there any good practice to prevent it?
 
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
 
pipenv Python virtual environments. Looks like a good item to use for separate development environments. virtual-environment-using-pipenv https://docs.python-guide.org/dev/virtualenvs/
$ pip install --user pipenv Use pip to install Pipenv:

pip python package manager install on FreeBSD 12
sudo pkg install py39-pip
# which pip-3.9
/usr/local/bin/pip-3.9

# ln -s /usr/local/bin/pip-3.9 /usr/local/bin/pip
# pip --version
pip 23.1.2 from /home/fred/.local/lib/python3.9/site-packages/pip (python 3.9)



How to install pip in python3 10?
Step 1: Download the get-pip.py (https://bootstrap.pypa.io/get-pip.py) file and store it in the same directory as python is installed. Step 2: Change the current path of the directory in the command line to the path of the directory where the above file exists. Step 4: Now wait through the installation process.Apr 17, 2023
PROBONPD talks about using pipenv and setting up python to build appimage-builder from his github.com site
https://github.com/probonopd/appimage-builder

$ pip --version
pip 23.1.2 from /home/fred/.local/lib/python3.9/site-packages/pip (python 3.9)

Upgrade pip:
# pip install --upgrade pip
 
Hello world,
Can i install python packages through pkg package manager
example : sudo pkg install py39-matplotlib
 
Can i install python packages through pkg package manager
This is the preferred method. That way system can keep track of package state.
Package audits will work correctly.

With PIP you need to watch the package. Only install what FreeBSD does not have in port tree.
 
Back
Top