VSCode & Python
Starting a New Python Project in VSCode
Starting a New Python Project in VSCode
Create a new folder for your project in the desired location using the Windows File Explorer.
Right-click on the project folder and select "Open with Code" from the context menu. This will open Visual Studio Code with the project folder as the current workspace.
Inside Visual Studio Code, click on "Terminal" in the menu bar, then select "New Terminal" to open the integrated terminal.
In the terminal, navigate to the folder where you want to create the virtual environment.
Before creating the virtual environment, check the version of Python you are using by running the command python --version. If the version is 3.10 or higher, you're good to go. If not, try running py --version or python3 --version to determine the latest version installed on your system. You may need to use py or python3 for the commands below.
Create a new virtual environment by using the command python -m venv .venv. This will create a new folder named ".venv" in the current directory.
Activate the virtual environment by running the command .venv\Scripts\activate.
In the terminal, verify the Python version again by running the command python --version to ensure it matches the version you selected before creating the virtual environment.
Create a new file with the ".py" file extension. This file will serve as your main Python file. You can name it "main.py" or any other preferred name.
Start writing your Python code in the main file.
To run the code, you can use the play button located at the top right corner or use the shortcut F5 to start a debug and run session.
To use (import) a module, you need to install it inside your virtual environment using pip. Follow the steps below:
If you haven't created the virtual environment, run the command py -3 -m venv .venv
If you're not already inside the virtual environment, activate it using the command .venv\scripts\activate
Upgrade pip by running python -m pip install --upgrade pip (The py -m command runs the pip tool from the terminal).
Install the desired module, e.g., pip install pygame
Test if pygame is installed correctly by running py -m pygame.examples.aliens
Note: Depending on your system, py, python, and python3 may point to different Python installations if multiple versions are present. Check the version using **** --version to ensure you're using the correct one. See below for more details.
Python commands in the terminal when multiple versions of Python are installed
In Python, "python" and "python3" are both command-line commands used to run Python code, but they have a few key differences:
"python" is a command that is used to run the default version of Python on a system. On Windows, this is typically Python 2.x, while on Linux and macOS, this is typically Python 3.x. If you want to run Python 2.x on Linux or macOS, you should use "python2" instead of "python".
"python3" is a command that is used to run the specific version of Python 3.x on a system. This command is typically used on systems where multiple versions of Python are installed, and you want to run a specific version of Python 3.x.
"py" is a command that allows you to run python on Windows, it is similar to python command but it is more specific to windows systems. If you have multiple versions of python installed in your windows machine, you can specify which version you want to use by running "py -3" or "py -2"
In summary, "python" and "python3" both allow you to run Python code, but "python" is used to run the default version of Python on a system, while "python3" is used to run a specific version of Python 3.x. On Windows, "py" can be used instead of "python" to run python.
Running Python Modules
In Python, the "-m" command is used to run a module as a script. It allows you to run a python module by specifying the module name, rather than the name of the script file.
For example, if you have a module named "mymodule" in your python environment, you can run it by using the command: python -m mymodule
This is equivalent to running a script named mymodule.py with the command: python mymodule.py
The -m command also allows you to run built-in modules that come with python, for example: python -m venv myenv
This command creates a new virtual environment in the folder myenv.
Another example is using the -m http.server command, this command starts a simple HTTP server in the current directory.
This feature is particularly useful when you have a module that you want to run as a script but don't want to create a separate script file. It also allows you to run built-in modules as script, which can be useful in various scenarios.
Created with the assistance of OpenAI's ChatGPT