If you are doing a system image upgrade, you arelikely to need to rebuild your virtualenvs.
There are two steps to this process; firstly, before you change the image, youwill need to gather information about the virtualenv. Then, after you havechanged it, you will be able to build a new one using that information.
The instructions below contain instructions for people who use plainvirtualenv
and virtualenvwrapper
; make sure that you use the appropriate onesfor the kind of virtualenv you're using. Use a Bash console to enter thecommands.
1) Before the system image change¶
Firstly, activate the virtualenv. If you're using virtualenvwrapper:
workon my-virtualenv-name
Or, if you're using a plain one:
source /home/myusername/path/to/virtualenv/bin/activate
Next, generate a requirements.txt
file to record what packages you're using.If you've already got a file like that, you can skip this, but otherwise:
pip freeze > /tmp/requirements.txt
Next, check which Python version you are using:
python --version
Finally, deactivate the virtualenv
deactivate
Now you have saved the information you need about the virtualenv, so you canchange the system image.
2) After the system image change¶
First of all, start a fresh Bash console.
We recommend that you create a new virtualenv with a different name, just incase something goes wrong in its creation.
The first step is to remove the .cache
directory from your home directory,because it may have versions of packages that pip will think are the right ones,but will only work with the old system image:
rm -rf ~/.cache/
The
mysqlclient
library is particularly prone to problems like this; ifafter the change over you start getting errors likeNameError: name '_mysql' is not defined
, it is likely that youhave a version of it that was installed from the cacheand is not compatible with the updated system image.
The next step is to identify the version of Python that you are going to use; checkthat the version that you identified when gathering data about your old virtualenvis available in the new system image by looking at the table at the bottom of thesystem images page. Note that the version that yougot above will be a full three-part version, for example 3.9.13. The numbers after the second "."are not important here; for example, if you got 3.9.13, any image that supported 3.9 would be OK.
Once you've determined the Python version, create the new virtualenv -- this willonly need the first two digits from the version number, eg. 3.9. If you're usingvirtualenvwrapper, create it like this:
mkvirtualenv --python=pythonX.Y my-new-virtualenv-name
Or, for a plain virtualenv:
virtualenv --python=pythonX.Y /home/myusername/path/to/new-virtualenv
Next, you can reinstall your packages. Ensure your new virtualenv is activated, then:
pip install -r /tmp/requirements.txt # or path to your existing requirements.txt
Once that's done, you can start using it.
If your virtualenv is used in a website, change the virtualenv setting on the "Web"page, and then click the green "Reload" button to restart the site using it.For always-on and scheduled tasks, change the command used to run them to pick up the new virtualenv-- you will normally have a workon
command, or use a specific path to the Pythoninterpreter to specify the virtualenv for those.
3) All done!¶
We're here to help! If you get stuck or confused, just drop us a note atsupport@pythonanywhere.com.