If you're new to programming or looking to dive into the world of Python, you'll need to install Python on your computer to get started.
How to install Python?
To install Python on your computer, follow these steps:
- Visit the official Python website: https://www.python.org/
- Navigate to the
Downloads
section. - Choose the version of Python you want to install.
- Select the appropriate installer for your operating system. Python is available for Windows, macOS, and Linux.
- For Windows: Download the executable installer (
.exe
file). - For macOS: Download the macOS installer (
.pkg
file). - For Linux: Most Linux distributions come with Python pre-installed. However, if you need a specific version or want to install Python from scratch, you can download the source code and follow the instructions provided.
- Once the installer is downloaded, run the executable file.
- On Windows, you may need to check the option "Add Python to PATH" during the installation.
- Follow the instructions in the installer to complete the installation process. You can generally accept the default settings.
- After the installation is complete, open the command prompt (on Windows) or the terminal (on macOS and Linux) and type
python --version
to verify that Python has been installed correctly. You should see the version number displayed.
That's it! You have successfully installed Python on your computer. You can now start writing and running Python programs.
How to Run a Python Script from a Command Line
To run a Python script from the command line, follow these steps:
- Open the command prompt (Windows) or the terminal (macOS/Linux).
- Navigate to the directory where your Python script is located. You can use the
cd
command followed by the directory path to change to the desired directory. For example, if your script is located in theDocuments
folder, you can use the following command:
cd Documents
- Once you are in the correct directory, you can run the Python script by typing
python
followed by the name of the script file. For example, if your script file is namedscript.py
, use the following command:
python script.py
If you have multiple versions of Python installed on your system, you may need to specify the version explicitly. In that case, use the following command instead:
python3 script.py
If you're using Python 2.x, the command would simply be
python
without the "3".
- Press the Enter key, and the Python script will execute. You will see the output or any error messages displayed in the command prompt or terminal.
That's it! You have successfully run a Python script from the command line. Remember to navigate to the correct directory where your script is located before executing the python
command.
Dive deep into the topic
Contribute with us!
Do not hesitate to contribute to Python tutorials on GitHub: create a fork, update content and issue a pull request.