Python development environment configuration and basic usage guide

Just installed Python and are overwhelmed by the empty terminal? scattered.pyFiles are scattered on the desktop. Want to switch to standard project mode but don’t know where to start? This article will use the most intuitive way to take you from "writing the first line of code" to "running the first project" to build a smooth and professional Python development environment.


1. Choose a comfortable editor/IDE

Python development tools can be roughly divided into two major groups: light editor and heavy IDE. There is no absolute advantage or disadvantage, the key depends on your stage and habits.

1.1 Visual Studio Code(VS Code)

  • Positioning: A lightweight and highly malleable editor that can almost turn into a small IDE after installing plug-ins.
  • Who is it suitable for: Friends who are just starting to learn Python, or who want to write front-end, script, Markdown and other types of files at the same time.
  • Killer Features: Fast startup, low memory usage, and extremely rich plug-in ecosystem (such as remote development, Docker, and Jupyter support).
  • Must do: Search for Python in the extension store and install the official Microsoft plug-in so that you can get smart completion, debugging and run buttons.

1.2 PyCharm (Community Edition is completely free)

  • Positioning: "Python Special" produced by JetBrains, a one-stop IDE that works out of the box.
  • Who is it suitable for: Students who are determined to delve into Python development (Web, crawlers, data analysis, automation) and hope to have top-level code tips, graphical debugging and virtual environment management.
  • Highlights: You no longer have to worry about Linter and Formatter, it is all configured for you; code completion and reconstruction capabilities are industry benchmarks, and the project-level management logic is very clear.
  • Must-know rules: PyCharm uses Project as the smallest management unit. You cannot directly drag one like using Notepad..pyJust get in and run - you have to build the project first.

1.3 Other options worth checking out

  • Sublime Text: It has good appearance and fast response, but it needs to be manually paired with the LSP plug-in to get smart completion.
  • Vim / Neovim: The ultimate weapon for keyboard geeks, the configuration threshold is high, but once you get started, the editing efficiency is incredible.

For novices, the Preferred PyCharm Community Edition can make it easier to get started; if the computer configuration is not high, or if you want to start lightly, VS Code is an excellent alternative.


2. Create your first Python project (take PyCharm as an example)

VS Code's project creation is relatively free, and you can start working by opening a folder. And we use PyCharm** demonstration with more rigorous project logic, which can help you establish a standardized project awareness.

Step 1: Create a new project

Open PyCharm and click New Project on the homepage. If you have already opened an old project, you can use the menu barFile → New ProjectEnter.

Step 2: Configure three core options

There are three key configurations in the new project panel. Try not to change the default values:

  • Location: Project storage path. It is recommended to create a dedicated folder, such asD:\PythonProjects\first_project, do not mix it with pictures and documents.
  • Python Interpreter: Must be selectedNew environment using Virtualenv! This will create an isolated Python environment for your project, so that future package installations will not interfere with each other, and will eradicate the incurable problem of "the library is obviously installed but it reports that it cannot be found".
  • Base interpreter: If your computer only has one Python version installed, just keep the default one; if there are several installed, choose the version you want to use (3.10 or above is recommended).

Step 3: Create.pydocument

Click Create and wait for PyCharm to complete initializing the environment. In the project bar on the left, right-click the project root directory (the folder with the same name as the last level of Location) and selectNew → Python File, namedhello

Note on file naming: Only use lowercase letters + underscores, such ashello.pydata_process.py;Don’t use Chinese characters, don’t include spaces, and don’t have the same name as a Python built-in module (such asos.pymath.py)。

Step 4: Write the first line of code

Openhello.py,enter:

print('hello, world')

Okay, now let's run it.


3. Three postures for running Python programs

When you run the code in PyCharm, you can directly enjoy the convenience of debugging, status bar prompts, etc.:

  • Right-click to run: Right-click in a blank space in the code editing area and selectRun 'hello'
  • Shortcut key to run: Windows/Linux pressShift + F10, macOS pressControl + R
  • View Results: The Run window at the bottom will printhello, world

If you use VS Code, after installing the Python plug-in, a small green triangle will appear in the upper right corner and you can click it to run it.

3.2 Method 2: Run from terminal command line (most common)

No matter what editor you use, whether there is an IDE or not, the command line is the "guaranteed solution" for running Python:

  1. Open Terminal (Windows PressWin + Rentercmd;macOS/Linux start Terminal directly);
  2. usecdEnterhello.pyThe folder where it is located, such as:
    cd D:\PythonProjects\first_project
  3. Execute:
    python hello.py

If you have installed multiple Python versions, you can use it on WindowspyThe command is more precise:py hello.py

3.3 Method 3: Direct execution on Unix system (macOS/Linux)

If you use macOS or Linux, you can add executable permissions to the script and call it directly like running a command:

  1. inhello.pyAdd the shebang line to the very beginning:
    #!/usr/bin/env python3
    print('hello, world')
  2. Grant execution permissions in the terminal:
    chmod +x hello.py
  3. Run:
    ./hello.py

This is a script that can be directly "clicked" or "entered the name to execute".


4. The difference between interactive mode, script mode and IDE running

When newbies come into contact with it for the first time, it is easy to distinguish the difference between "typing Python in the terminal" and "running a file". It will be clear by looking at this table:

FeaturesInteractive mode (REPL)Script mode (.py file)IDE run (PyCharm/VS Code)
Execution methodEnter line by line, enter one line and press Enter to get the resultExecute the entire file in one goCall the script mode behind the scenes, and integrate debugging and variable monitoring
What is suitable forTemporarily testing a function, quick calculationWriting formal projects, need to save codeProfessional development, complex debugging, team collaboration
Code retentionClose the terminal and it will be gonePermanently saved in filesSaved in files and project configurations
How to enterDirect input from terminalpythonEnterEditor opens.pyfile, or execute it from the command lineRight-click/shortcut key to run in the IDE

Simply remember: **Use interactive mode to write small experiments, use script mode to write formal projects, and run directly in IDE for daily development. **


5. Beginner’s Guide to Preventing Pitfalls & Golden Practices

5.1 Three high-frequency error reports and quick rescue

  1. Errno 2 No such file or directory
    File not found.cdswitch to and.pyfile in the same directory and run again; or check the spelling of the file name.

  2. 'python' 不是内部或外部命令
    Forgot to check Add Python X.X to PATH when installing Python. Rerun the installation program and check it; or manually add the Python path to the system environment variable.

  3. PyCharm displays “No Interpreter” in the lower right corner Click on the prompt there and selectAdd Interpreter → Existing environment, navigate to the Python installation path; or re-create a Virtualenv environment in Project Settings.

5.2 Three norms that must be followed

  • File name specification: uniformlowercase_with_underscore.py. Chinese characters, spaces, and the same name as the standard library are prohibited.
  • Iron Law of Virtual Environment: Each project has an independent virtual environment. PyCharm does this for you by default when building a project; using VS Code or other editors, you can execute it in the project folder:
    python -m venv venv

Then usesource venv/bin/activate(For Windowsvenv\Scripts\activate)activation.

  • Chinese encoding statement: If you need to write Chinese comments or output Chinese, it is recommended to add a line of encoding statement at the beginning of the file (Python 3 can be omitted, but adding it will make it clearer):
    # -*- coding: utf-8 -*-
    print('你好,世界!')

Conclusion

From now on, you already have a clean, isolated, and reusable Python development environment. By following this article, you will not only be able to write and runhello, world, also understood the interactive mode, script mode and the underlying logic of IDE operation. Next, feel free to write your first crawler, first data analysis script, or first automation tool - the good environment is already in place, and all that's left is to do it!