The -m option runs a Python module by its module name rather than by giving a source-file path.
Understanding the Concept
A module is a Python file or package containing reusable code. The -m option asks Python to locate that module using its module search mechanism and run it as a program when supported. It is particularly useful for built-in tools such as venv and pip. For example, python -m pip makes it clear which interpreter is managing packages, and python -m venv .venv uses the selected interpreter to create an environment.
When and Why You Would Use This
Use your actual Python command before -m. A module that is not installed or cannot be executed this way will produce an error. The option does not download modules or make every library executable.
Practical Example
python -m pip --version
python -m venv .venv
Understanding the Example
Run these in a shell using an interpreter already verified as Python. The first displays the pip version. The second creates a virtual environment inside the current project folder. Do not paste these commands into a Python source file.
Try It Yourself
Run python -m pip --version inside a project terminal and compare the Python environment with the one selected in PyCharm.
Common Beginner Mistakes
- Confusing
-mwith a Python function that belongs inside a .py file. - Assuming a module name is a filename path; modules are resolved through Python import rules.
- Using a different interpreter for pip and project execution.
Key Takeaways
python -mexecutes a module through the selected interpreter.- It is useful for pip, venv, and other module-based tools.
- Module names and file paths are different ways of selecting what to run.