Issue w/ pipenv python versions after upgrading asdf’s python

I recently upgraded from python 3.9.12 to 3.9.14 using asdf, and it was pretty simple:

sudo apt-get install liblzma-dev
asdf install python 3.9.14
asdf global python 3.9.14

But when I tried to run my pipenv, it didn’t work as planned

pipenv shell
No preset version installed for command pipenv
Please install a version by running one of the following:

asdf install python 3.9.14

or add one of the following versions in your config file at /home/mark/.tool-versions
python 3.9.12

After a lot of failed attemps to use asdf reshim and asdf update, I finally remembered the magic steps to use:

python -m pip install --upgrade pip
pip install pipenv

asdf update
asdf reshim python 3.9.14   # or whatever newest version is

pipenv install
pipenv shell
pipenv update   # To actually install what was in Pipfile

The last trick that I needed to do was make sure that my .env gets read so that any Python environment variables I set are correctly read in.

vi ~/.local/share/virtualenvs/hostname-RANDOM/bin/activate

And add these 2 lines starting at line 4 of the activate script:

# Use this sequence to load up the environnment variables from your .env file in a Bash console:
#
set -a; source /home/mark/home/aei/aeicloud.22/.env; set +a

Ubuntu 22 LTS on WSL / asdf / python / django

I’ll be using asdf to help control what version of Python I’m running.

I like pipenv to help manage my Python virtual environments.

I like to use PostgreSQL with Django, so lets’ first make sure that it’s installed:

sudo apt install postgresql 
sudo apt install libpq-dev

# make sure it's running
# You should see it listening to lots of ports
ss -nlt

asdf

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.0

# modify .bashrc files, exit terminal and restart terminal
sudo apt-get update
sudo apt-get upgrade

# install packages necessary to build stuff
sudo apt install wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev liblzma-dev

asdf plugin-add python

asdf install python 3.9.12
asdf global python 3.9.12

If the correct version of Python isn’t detected, you might need to reshim.

python -m pip install --upgrade pip
pip install pipenv

asdf update
asdf reshim python 3.9.12

pipenv install

[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
You can use $ pipenv install –skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
Hint: try $ pipenv lock –pre if it is a pre-release dependency.
ERROR: metadata generation failed

Turned out the above error, was because I didn’t have postgres installed on the new box (and my django uses postgres)

 Error: pg_config executable not found.

I got much futher along

# how to know things are almost all the way there
./manage.py shell
python manage.py dbshell

# If you didn't setup your postgres, you'll get an error like:
# python manage.py dbshell
# psql: error: connection to server at "127.0.0.1", port 5432 failed: Connection refused
#         Is the server running on that host and accepting TCP/IP connections?

You’ll need to start the server:

sudo service postgresql start

Then try

python manage.py dbshell
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL:  password authentication failed for user "abcuser"
connection to server at "127.0.0.1", port 5432 failed: FATAL:  password authentication failed for user "abcuser"

The user and database don’t appear to exist — so let’s create them:

sudo su - postgres
# skipping the lines as you login as user postgres


psql
# now you are in the postgres shell

CREATE DATABASE abcdb;
CREATE USER abcuser WITH PASSWORD 'abc123';

\q     # that exists the postgres shell

exit   # that logs out the postgres user

You should now be able to init the DB

python manage.py migrate
python manage.py createsuperuser

# then run any bootstrapping that you need to
# python manage.py loaddata  xyz.json
# needed for pango and weasyprint (creating PDFs)
sudo apt-get install libpangocairo-1.0-0

Now, to get django working in VSCode

  1. install the Python extension for Visual Studio Code
  2. Press F1, then choose Python: Select Interpreter and make sure you are pointing to your correct venv
  3. Press the Run and Debug Icon