Frequently Asked Questions for Python Beginners II


foreword

It is still a common problem for python beginners, from the reference link


1. python xxx.py and python3 xxx.py

  • If only Python 2 or Python 3 is installed in your computer, in short, there is only one Python, then, no matter which system you are, you can always use the python xxx.py
    form to run the code through the corresponding Python interpreter.
  • If your computer is macOS or Linux, as long as your computer has Python 3, whether you have Python 2 or not, you can always use python3 xxx.py to run code through Python 3. At this time, if there is Python 2, when python xxx.py is executed, the code is run through Python 2.
  • If your computer is Windows, one version of Python is installed first, and then another version of Python is installed, then I suggest you do this: enter the installation folder of Python 3, and rename the python.exe inside to python3. exe. In this way, you can always run the code with Python 3 by executing the command python3 xxx.py; execute the code with Python 2 by executing the command python xxx.py.
  • My computer has both Python 2 and Python 3, so where are the third-party libraries I installed using pip installed?
    It depends on which version of Python pip is found first when you execute pip. The system searches one by one according to the path in the environment variable. If one is found first, it will be used immediately. no longer look for the back

2. Command line and Python interactive environment

  • What is a command line, and what is a Python interactive environment?
    In Windows, you directly open CMD, Powershell, or open a terminal on macOS or Linux. The black window you see is called a command line, and commands are executed here. The command line of CMD generally starts with the file path plus a right arrow, and the command line of macOS and Linux generally starts with the $ symbol.
    When you enter python3 in the terminal and press Enter, you open the Python interactive environment, which starts with three right arrows: >>> Python
    code is executed in the Python interactive environment, not the shell Order.

3. Work area

Python has a concept of workspace. By default, when you use python xxx.py to run a .py file, the workspace is the folder where the .py file you run is located. Since the login.py and main.py files are placed in the same folder, when you run main.py directly, Python can correctly know that from login import login means from and main.py are in the same folder Import the login function in this login.py file. So everything is normal.
But when you use PyCharm to open a project folder, since a file in the project has not been run, PyCharm will use the currently opened project folder as the workspace.
It is also very simple to solve this problem. Open PyCharm's project settings, locate ProjectStructure, delete the current content, and add new content (workspace)


Summarize

The above is what I will talk about today. This article only briefly introduces the python work path problem

Guess you like

Origin blog.csdn.net/goodlmoney/article/details/126772967