Python Setting python modules path?

FreeBSD 13.1-REL
Python 3.11 (installed from packages)
VS Code OSS 1.70.2 (installed from packages)

I'm a beginner python programmer, so please be gentle. I've been working through Python exercises using the VS Code IDE. Although FreeBSD had Python 3.9 baked-in, I wanted to learn on the latest version so I installed Python 3.11. So now I have both it and 3.9 co-existing. That has seemed fine.

I'm learning to use pytest now, and while the tutorials instruct installing it via pip, it seems pip on FreeBSD is a bit of a minefield. It seems the recommended way is to install things from packages/ports when possible. I did find py-pytest in packages so I installed that. It runs fine from the command line manually, although it's configured to use Python 3.9. That hasn't seemed to be an issue.

However, now I need to be able to include pytest in code while working in the VS Code IDE. It doesn't seem to be able to find pytest using its normal methods. I do have the Python path set to /usr/local/bin/python3.11 which has been working fine. I also see that pytest is installed in /usr/local/bin . How do I get python3.11 (or perhaps it's VS Code) to be able to find pytest so that I can "import pytest"?

Thanks!
 
The modules for "import" are found in /usr/local/lib/pythonX.Y/..., but which python interpreter is run by default is decided by the series of softlinks python -> python3 -> python3.X.

You could try to use the pytest imports for version 3.9 with the rest of the 3.11 installation. Read about the "module path". I think that is a bad idea, and will probably cause bizarre problems that will be hard to debug.

Since you are a beginner, I would suggest: SIMPLIFY. Why exactly do you want to use 3.11? Can you list a specific feature that you really want or need? If not, why not do your studying on just 3.9? The differences really aren't very big. Stick to 3.9 and use it consistently, getting most python add-ons from packages.

On the other hand, if you really want 3.11, then uninstall python 3.9 completely, install 3.11 from scratch, and then use pip (more accurately pip3) for everything, and you will have a pure 3.11 installation. While installing modules using packages is indeed easier, pip works too. Mixing the two causes issues during upgrades, and if you forget which part was installed how, you end up breaking things when upgrading carelessly.

Personally, after some "bad experiences" during upgrades, I've made it a policy to stick to one installed python version on my machine, and I mostly rely on packages.
 
Hey there, Ralph. Thanks for the quick reply!

I wanted to use Python 3.11 because the book I'm working through was designed for 3.10. So it seemed safer to work with 3.11 than 3.9.

I worry about removing 3.9 and trying to hamfist/shoehorn everything to use 3.11 due to the number of things installed that seemed to be hard-coded for 3.9:

Code:
py39-attrs-22.1.0              Python attributes without boilerplate
py39-cairo-1.21.0,1            Python bindings for Cairo
py39-dnspython-2.2.1_1,1       DNS toolkit for Python
py39-evdev-1.6.0               Bindings to the Linux input handling subsystem
py39-gobject3-3.42.2           Python 3.9 bindings for GObject
py39-importlib-metadata-4.8.1  Read metadata from Python packages
py39-iniconfig-1.1.1_1         Simple parsing of ini files in Python
py39-markdown-3.3.7            Python implementation of Markdown
py39-packaging-21.3            Core utilities for Python packages
py39-pluggy-1.0.0              Plugin and hook calling mechanisms for Python
py39-py-1.11.0                 Library with cross-python path, ini-parsing, io, code, log facilities
py39-pyparsing-3.0.9           General parsing module for Python
py39-pytest-7.1.3,1            Simple powerful testing with Python
py39-pyudev-0.22.0             Pure Python libudev binding
py39-setuptools-63.1.0         Python packages installer
py39-six-1.16.0                Python 2 and 3 compatibility utilities
py39-tomli-2.0.1               Lil' TOML parser
py39-zipp-3.4.0                Backport of pathlib-compatible object wrapper for zip files

I have no problem with all internal/system things to continue to use 3.9, if I could just find some way for 3.11 to find pytest.
 
Install lang/python310 (as the book is based on it and it should happily live next to Python 3.9) and use a venv to install the modules you need.

Another option, but this is a lot more involved, is to set the default Python version to 3.10 (or 3.11) and build everything from ports. The official packages have the default Python set to 3.9 but you can change that default if you build from ports.

Add to make.conf:
Code:
DEFAULT_VERSIONS+= python=3.10
 
Back
Top