Catégories

Python virtual environment made easy

Temps de lecture : 3 minutes

I use Windows 10 22H2 but the process should be similar on other Windows versions or OSes. I suppose Python 3 is working.

Quick Commands

1 - In a console

  • mkdir "My Project"
  • cd '.\My Project\'
    Type "cd My" then strike TAB
  • py -m venv .\PrjVEnv
    This may take few seconds
    This creates, among other things, a subdirectory named PrjEnv
  • ls
    Just to check that the PrjEnv subdirectory has been created
  • code .
    Don't forget the dot
    This launches VSCode "in" the current directory
Click on the image to see it full size

2 - In VSCODE

  • CTRL + SHIFT + P
  • Python: Select Interpreter 
    If needed start to type "Python" then the different options should be listed.
  • Python 3.10.7 ('PrjVEnv':venv)./PrjVEnv/Scripts/python.exe
    Or you run another Python version pick the one listed with the "('PrjVEnv':venv)"
    This is the interpreter running within the context of the Python Virtual Environment
  • CTRL + SHIFT + ù
    To open a new Terminal within VSCode
At the very beginning of the prompt, you should see (PrjEnv) in green
  • python -m pip install numpy
    This installs the lib needed by the project

Done!

Just to make sure...

  • Enter some code
import numpy as np

def MyFunction():
  A = np.array([0, 1, 2, 3])
  print(A.shape, A.size)

if __name__ == '__main__':
  MyFunction()
  • CTRL+S
    Save the code
  • Set a breakpoint
  • F5
    Select "Debug the currently active Python file"
  • F10
    To step over the breakpoint when you reach it
  • Inspect the variables etc.
  • F5
    To let the code ends
  • If additional libs are needed, as before, add them from the VSCODE console
  • python -m pip install scipy
    For example

Voilà, I hope this helps.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.