Home ยป MacOS / Linux: How to start a new Python 3 project using VENV Virtual Environment

MacOS / Linux: How to start a new Python 3 project using VENV Virtual Environment

I thought I would write a brief blog to capture the notes I use when I create a new Python 3 project. I’m working on MacOS but the process should be the same on other Unix/Linux based systems and the principles should be the same if you are working on Windows.

Step 1: Create a new directory to house your project

mkdir my-project

cd my-project

Step 2: Create a Python virtual environment so we can create a local set of dependancies for this project.

python3 -m venv venv

Step 3: Activate your virtual environment

source venv/bin/activate

Step 4: If you have a ‘requirements.txt’ listing the dependancies for the new project run it now.

pip install -r requirements.txt

check the list afterwards using pip list

Step 5: Create some new Python source

Don’t put your own source in the venv directory, keep it in the parent directory. For example

cd my-project

touch demo.py

code demo.py

The code command is the command line shortcut to launch Microsoft Visual Studio Code.

Step 6: If you are using VS Code, it doesn’t seem to detect the virtual environment automatically all the time. So you need to manually tell it which environment to use for your code.

In Code do CMD-SHIFT-P then search for Python: Select Interpreter. Look down the list and select the Python executable in the venv directory we created in step 2.

Step 7: Set-up GIT version control and omit the venv directory in your .getignore file.

Notes:

  • If you want to leave your VENV virtual environment at any time, you can either switch to another environment using the activate step shown above or you can exit completely back to your systems default Python using the deactivate command.
  • If you don’t have the VENV command installed in your Python environment use the command pip3 install virtualenv to install it.




You May Also Like…

Find similar blogs in these categories: Python
0 Comments
Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.