Both Python 2 and Python 3 installed on MacOS, but pip command install libraries only for Python 3

I have both Python 2 and Python 3 installed on my MacOS (Mojave 10.14.5). When I run my Python 2 code I get:
ImportError: No module named <modulename>
if I install the module with pip install <modulename> command i get:
Requirement already satisfied: <modulename> in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (0.5)
Then same missing package message as before when running the code with Python 2. The command pip2 install <modulename> is not working. How could I install modules for Python 2?

You can ensure you run pip for Python 2 by invoking it as python2 -m pip ....

Thank for your response, unfortunately from the terminal i get:
-bash: python2: command not found

Then use whatever the Python 2 command is called on your system.

python pip install snap python: can't open file 'pip': [Errno 2] No such file or directory
pip2 is not working

You left out the -m option between python and pip.

Ok, I managed to install the module with python -m pip install --user the_module. Now another problem, maybe worst: I need to install the package cvxpy which I think requires at least python 3.7 because It says RuntimeError: Python version >= 3.7 required. Is there any possibility to get around this?

That suggests you are not trying to run the project using Python 3.7 or newer which the project requires. You need to use a newer version of Python, otherwise you will need to personally port the project to whatever version of Python you need support on.