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
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
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
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.
- 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.