Background #
Due to the default configuration of Python virtual environments using absolute paths, it is not possible to move a virtual environment directory by simply copying or moving files after it has been created. In this article, I will introduce two methods that I commonly use when I need to move a file structure, as well as a third method that requires the installation of an additional Python package. However, I do not frequently use the third method and only provide it for reference.
Creating a Practice Virtual Environment and Test Materials #
The system I am using while writing this article is MacOS 14.6, with a global Python version of Python 3.12.6 (which can be checked using the command python3 --version
).
First, let’s create the files that will be used for demonstration in this article: mkdir -p ~/Developer/drafts/{Venv_1, Venv_2}
. This will create the following directory structure:
Developer/
├── drafts/
│ ├── Venv_1/
│ └── Venv_2/
Next, run the following command to create a Python virtual environment in the Venv_1/ directory: python3 -m venv ~/Developer/drafts/Venv_1
Enter the directory and activate the virtual environment:
cd ~/Developer/drafts/Venv_1
source bin/activate
After activation, you will see (Venv_1) appear in front of the terminal prompt, indicating that you are in the virtual environment. Now, let’s install three commonly used Python packages for practice:
pip install requests
pip install beautifulsoup4
pip install pytz
After the installation is complete, you can run the following command to check if these packages have been successfully installed: pip list
.
Then, let’s write a simple Python program in the Venv_1/ folder to test the usage of the virtual environment:
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import pytz
# Send a GET request to the Python tutorial page
url = 'https://docs.python.org/3/tutorial/'
response = requests.get(url)
# Use BeautifulSoup to parse the HTML
soup = BeautifulSoup(response.text, 'html.parser')
# Find the title of the page
title = soup.find('title').text
# Get the current UTC time
utc_now = datetime.now(pytz.utc)
# Format the time string
formatted_time = utc_now.strftime('%Y-%m-%d %H:%M:%S %Z')
# Print the results
print(f'Page title: {title}')
print(f'Current UTC time: {formatted_time}')
Please save this code to a file (e.g., test_packages.py) and run it while the Venv_1 virtual environment is activated: python test_packages.py
.
If everything is working correctly, you should see output similar to the following:
Page title: The Python Tutorial — Python 3.x.y documentation
Current UTC time: 2023-04-18 12:34:56 UTC
Moving the Virtual Environment #
Method 1: Create a New Virtual Environment and Bulk Import Previous Python Packages #
- With Venv_1 activated, use the command
pip freeze > requirements.txt
to export the packages from the old environment to a requirements.txt file. - Use
deactivate
to exit the Venv_1 virtual environment. - Use the command
python3 -m venv ~/Developer/drafts/Venv_2
to create a new virtual environment in the Venv_2 folder. - Use the command
cp ~/Developer/drafts/Venv_1/requirements.txt ~/Developer/drafts/Venv_2/requirements.txt
to copy the txt file storing the old environment packages to the new environment. - Move to the new environment folder and activate the new environment:
cd ~/Developer/drafts/Venv_2
,source bin/activate
. - Use the command
pip install -r requirements.txt
to bulk install the Python packages. - Copy the Python files from the old environment to the new environment and test if the installation is working correctly. You can also use the command
pip list
to check the installation status of the Python packages or use the commandwhich python
to see the address of the currently used Python.
Method 2: Copy the Entire Environment and Modify Relevant Configuration Files #
- Navigate to the drafts/ directory:
cd ~/Developer/drafts
. - Move the entire contents of Venv_1/ to Venv_2/:
cp -r Venv_1/* Venv_2
. - Enter the Venv_2 folder:
cd Venv_2
. - Modify the paths and prompt variables in the bin/activate file:
sed -i '' 's/drafts\/Venv_1/drafts\/Venv_2/g' bin/activate
sed -i '' 's/VIRTUAL_ENV_PROMPT="(Venv_1) "/VIRTUAL_ENV_PROMPT="(Venv_2) "/g' bin/activate
- Modify the home path in the pyvenv.cfg file:
sed -i '' 's/drafts\/Venv_1/drafts\/Venv_2/g' pyvenv.cfg
- Activate the modified virtual environment:
source bin/activate
- Verify if the virtual environment is working correctly. You can run the
test_packages.py
script again:
python test_packages.py
You can also use the command pip list
to check the installation status of the Python packages or use the command which python
to see the address of the currently used Python.
Method 3: Use the virtualenv Tool #
virtualenv allows you to create a clone or copy of a virtual environment. This is a relatively simple and straightforward method that does not require manual modification of the virtual environment’s configuration files.
- First, make sure you have virtualenv installed. If not, you can install it using pip:
pip install virtualenv
. - Deactivate the current virtual environment (if you are in one):
deactivate
. - Use the –relocatable option of virtualenv to set the existing virtual environment Venv_1 as relocatable:
virtualenv --relocatable ~/Developer/drafts/Venv_1
. This will make some of the files created by setuptools use relative paths and will change all the scripts to use activate_this.py instead of using the location of the Python interpreter to select the environment. Note: Scripts that have been made relocatable will only work if the virtualenv is activated, specifically the python executable from the virtualenv must be the first one on the system PATH. Also note that the activate scripts are not currently made relocatable byvirtualenv --relocatable
. - Use the –relocatable option of virtualenv to clone Venv_1 to a new location Venv_2:
virtualenv --relocatable ~/Developer/drafts/Venv_1 -p python3 ~/Developer/drafts/Venv_2
. This command will create a new virtual environment Venv_2 that is a clone of Venv_1. The -p python3 option specifies the Python interpreter to use. - Activate the new virtual environment:
source ~/Developer/drafts/Venv_2/bin/activate
. - Verify if the virtual environment is working correctly. You can run the
test_packages.py
script again:python test_packages.py
You can also use the commandpip list
to check the installation status of the Python packages or use the commandwhich python
to see the address of the currently used Python.
Notes #
- For convenience and to ignore the current directory location, most commands in this article use absolute paths.
- Personally, I most frequently use the second method. If you encounter any issues with the other methods, you can contact me via email for modifications: StepaniaH0907@gmail.com.
- If you know of a more convenient way to move virtual environments, I would appreciate it if you could teach me. Thank you.