Python3.12 Upgrade: Setting Up Virtualenvwrapper

Search for a command to run...

No comments yet. Be the first to comment.
Over the years, my journey through the innovation landscape has been nothing short of exhilarating. From working in cutting-edge innovation labs to collaborating closely with venture capitalists, venture builders, and factories, I’ve seen the power o...

After years of taking over the maintenance of pychartjs, I'm excited to finally have the time to bring some much-needed updates to the project. It's been a journey, but I'm proud to say that pychartjs is now framework-agnostic and fully compatible wi...

Introduction Recently, I worked on a project that needed many gRPC functions. If you've used gRPC, you know it helps different software programs talk to each other efficiently. However, making Proto files for every Python dataclass was taking a lot o...

I wanted to share some thoughts on the importance of code reviews, appreciating your teammates, and this cool tool I found called Git Add Co-Author that makes giving credit where it’s due super easy. A few weeks ago, I wrote a blog post titled "Contr...

Hello everyone, I hope you are doing well. I want to share some important news with you. I am nominating myself for the position of Director on the Board of the Python Software Foundation (PSF). My Statement Here is my official statement for the PSF ...

Recently, I decided to upgrade my system to Ubuntu 24.04. I enjoy keeping my setup current, and I wanted to take advantage of the latest features and improvements. Everything seemed to be going well until I hit a snag. I use virtualenvwrapper to manage my Python environments, but after the upgrade, it stopped working. I kept getting this error:
/usr/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is set properly.
This was a new problem for me. I discovered that Python 3.12, which comes with Ubuntu 24.04, has stricter rules about installing packages globally. This change is designed to prevent conflicts and maintain system stability by keeping the system's Python environment clean. However, it also means you can't just use pip to install things globally like before. Instead, you have to use virtual environments for package management.
I initially tried using pipx to install virtualenvwrapper, but I ran into another issue. The virtualenvwrapper installed via pipx wasn’t accessible in my .zshrc configuration. It seems pipx is great for some standalone applications, but it doesn't play well with tools like virtualenvwrapper that need to be sourced in a shell configuration file.
To fix this, I decided to install virtualenvwrapper in its own virtual environment. Here’s how I did it:
First, I created a new virtual environment to keep things isolated.
python3 -m venv ~/.virtualenvs/venv
This command made a new virtual environment in the ~/.virtualenvs/venv directory.
Next, I activated the virtual environment.
source ~/.virtualenvs/venv/bin/activate
The shell prompt changed, showing I was now working inside the venv environment.
virtualenvwrapperWith the virtual environment activated, I installed virtualenvwrapper.
pip install virtualenvwrapper
Why Do This in a Virtual Environment?
Avoid Conflicts: Python 3.12 doesn’t let you install packages globally to avoid messing up the system’s packages. Using a virtual environment keeps everything separate.
Isolation: By installing in a virtual environment, virtualenvwrapper gets its own set of dependencies, with no interference from other projects.
Easy Management: If something breaks, you can just delete the virtual environment and start over, without affecting the system Python or other projects.
Next, I needed to tell my shell to use this new setup. I edited my .zshrc file (it could be .bashrc if you use bash).
.zshrcI opened my shell configuration file in a text editor:
nano ~/.zshrc
I added these lines:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=$HOME/.virtualenvs/venv/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=$HOME/.virtualenvs/venv/bin/virtualenv
source $HOME/.virtualenvs/venv/bin/virtualenvwrapper.sh
What Do These Lines Do?
export WORKON_HOME=$HOME/.virtualenvs: This sets the directory where virtualenvwrapper will keep all my virtual environments.
export VIRTUALENVWRAPPER_PYTHON=$HOME/.virtualenvs/venv/bin/python: This tells virtualenvwrapper to use the Python from my virtual environment.
export VIRTUALENVWRAPPER_VIRTUALENV=$HOME/.virtualenvs/venv/bin/virtualenv: This points to the virtualenv executable in my virtual environment.
source $HOME/.virtualenvs/venv/bin/virtualenvwrapper.sh: This initializes virtualenvwrapper so I can use its commands.
In nano, I saved the file by pressing Ctrl+O and then Enter, and exited with Ctrl+X.
To apply the changes, I reloaded my shell configuration:
source ~/.zshrc
Finally, I checked to see if everything was set up correctly:
which mkvirtualenv
This command showed the path to the mkvirtualenv script in my virtual environment, confirming that everything was working.
Upgrading to Ubuntu 24.04 brought a little challenge with virtualenvwrapper, but I learned a lot by solving it. By using a dedicated virtual environment, I kept my system clean and my projects organized. If you run into the same issue, I hope this guide helps you out. Happy coding!