Python Tutorial (3) - Running Python

Time: Column:Python views:656

Below are some important environment variables that apply to Python:

Variable NameDescription
PYTHONPATHPYTHONPATH is the Python search path. By default, the modules we import are searched in PYTHONPATH.
PYTHONSTARTUPWhen Python starts, it looks for the PYTHONSTARTUP environment variable and then executes the code in the file specified by this variable.
PYTHONCASEOKIf the PYTHONCASEOK environment variable is set, Python will ignore case sensitivity when importing modules.
PYTHONHOMEAn alternative module search path. It's usually embedded in the PYTHONSTARTUP or PYTHONPATH directory, making it easier to switch between two module libraries.

Running Python

There are three ways to run Python:

1. Interactive Interpreter:

You can enter Python through the command-line window and start writing Python code in the interactive interpreter.

You can work with Python in Unix, DOS, or any other system that provides a command line or shell.

$ python             # Unix/Linux


or

C:>python           # Windows/DOS


Below are some Python command-line options:

OptionDescription
-dDisplay debugging information during parsing
-OGenerate optimized code (.pyo files)
-SDo not import the location to find Python paths on startup
-VOutput the Python version number
-XFrom version 1.6 onwards, based on built-in exceptions (deprecated for string handling)
-c cmdExecute Python scripts and run the result as the cmd string
fileExecute Python scripts from the specified Python file

2. Command-Line Script

You can execute Python scripts from the command line by invoking the interpreter in your application, as shown below:

$ python script.py          # Unix/Linux

or

C:>python script.py         # Windows/DOS

Note: Make sure the script has executable permissions when executing the script.

3. Integrated Development Environment (IDE): PyCharm

PyCharm is a Python IDE developed by JetBrains, supporting macOS, Windows, and Linux systems.

PyCharm features include: debugging, syntax highlighting, project management, code navigation, intelligent suggestions, auto-completion, unit testing, version control, and more...

Python3 Tutorial (3) - Running Python

PyCharm interface:

Python3 Tutorial (3) - Running Python

Install the PyCharm Chinese plug-in, open the menu bar File, select Settings, then select Plugins, click Marketplace, search for Chinese, and then click install to install:

Python3 Tutorial (3) - Running Python

Anaconda Integrated Environment

The Anaconda distribution includes Python.

Anaconda is an integrated environment for data science and machine learning, including the Python interpreter and many commonly used data science libraries and tools.

The management tool for Anaconda packages, dependencies, and environments is the conda command. Compared to the traditional Python pip tool, Anaconda's conda allows for easier switching between different environments and simpler environment management.