The main difference is the virtual environment directory name and visibility. Both approaches work identically, they create isolated Python environments with their own packages and dependencies. The activation commands differ accordingly:
.venv/bin/activatefor the hidden versionvenv/bin/activatefor the visible version
python3 -m venv .venv creates a hidden
directory named .venv (the leading dot makes it hidden on
Unix systems like macOS). When you run ls in your project
directory, you won’t see it unless you use ls -a to show
hidden files.
python3 -m venv venv creates a visible
directory named venv that will show up in normal directory
listings.
Many developers prefer .venv because:
- It keeps the project directory cleaner visually
- It’s less likely to be accidentally committed to version control
(though you should add it to
.gitignoreeither way) - It follows the convention of hidden configuration files
Example 1
python3 -m venv .venv
source .venv/bin/activate
pip install pyyaml
To exit from a Python virtual environment, simply type
deactivate
Example 2
# Create a virtual environment
python3 -m venv venv
source venv/bin/activate
# Install beautifulsoup4
pip install beautifulsoup4
# Run the script
python search_generator.py ./pages
# Deactivate when done
deactivate