0

I have been struggling to get my new python environment to work so I was wondering if anyone can share some time for help.

So I have install pyenv in order to work with different versions of Python.

Pyenv installation was successful and I was able to switch between different versions of python. But when I run aws-adfs to login, I've got a ModuleNotFoundError:

Input: aws-adfs login --adfs-host=####### --session-duration 43200 --profile=leo

Output:

Traceback (most recent call last):
File "/Users/leol/.pyenv/shims/aws-adfs", line 5, in <module>
from aws_adfs.commands import cli
File "/usr/local/lib/python3.9/site-packages/aws_adfs/commands.py", line 8, in <module>
from . import list_profiles
File "/usr/local/lib/python3.9/site-packages/aws_adfs/list_profiles.py", line 5, in <module>
from .prepare import create_adfs_default_config
File "/usr/local/lib/python3.9/site-packages/aws_adfs/prepare.py", line 4, in <module>
import botocore.session
File "/usr/local/lib/python3.9/site-packages/botocore/session.py", line 29, in <module>
import botocore.configloader
File "/usr/local/lib/python3.9/site-packages/botocore/configloader.py", line 19, in <module>
from botocore.compat import six
File "/usr/local/lib/python3.9/site-packages/botocore/compat.py", line 26, in <module>
from dateutil.tz import tzlocal
File "/usr/local/lib/python3.9/site-packages/dateutil/tz/__init__.py", line 2, in <module>
from .tz import *
File "/usr/local/lib/python3.9/site-packages/dateutil/tz/tz.py", line 19, in <module>
import six
ModuleNotFoundError: No module named 'six'

My .bashrc settings:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init --path)"
fi

eval "$(pyenv virtualenv-init -)"

When running pip install six, I got:

Requirement already satisfied: six in /Users/leol/.pyenv/versions/3.9.7/lib/python3.9/site-packages

My initial thought was that because of pyenv I am no longer using libraries inside this directory /usr/local/lib/python3.9/site-packages/aws_adfs/commands.py. But I wasn't sure if this is the correct issue nor how to resolve it.

Can anyone help me?

leol
  • 37
  • 6

2 Answers2

1

After hours of struggling, I was able to confirm that my hypothesis was correct. So I've added some more code in the .bashrc file to help python to use the correct modules depending on the python version that I use.

export PYENVVERSION="$(pyenv global)" #3.9.7
export PYENVVERSIONRG="$([[ $PYENVVERSION =~ (^[0-9]\.[0-9]) ]] && echo $BASH_REMATCH)" # 3.9
export PYTHONPATH="$PYTHONPATH:$PYENV_ROOT/versions/$PYENVVERSION/lib/python$PYENVVERSIONRG/site-packages" #/Users/leol/.pyenv/versions/3.9.7/lib/python3.9/site-packages

I have not tested this with other version of python yet but should be sufficient to handle (as long as you have corresponding library installed)

Hope this answer will help someone someday.

leol
  • 37
  • 6
0

If anyone else is using zshell (default mac shell) leol's answer will work with one modification. zshell stores regex matches in the array $match. Also I added a lil' check to see if you have a python path set.

export PYENVVERSION="$(pyenv global)"
[[ $PYENVVERSION =~ (^[0-9].[0-9]*) ]] && PYENVVERSIONRG=$match[1]

PYTHONPATHPRE=""
if [ ! -z "$PYTHONPATH" ]
then
        PYTHONPATHPRE="$PYTHONPATH:"
fi

export PYTHONPATH="$PYTHONPATHPRE$PYENV_ROOT/versions/$PYENVVERSION/lib/python$PYENVVERSIONRG/site-packages"