For a while Python After language , A lot of people started writing complex software , Now the code is written in a file , It's very unlikely that . in addition , It's impossible for me to write all the function codes , What's more, the boss's deadline. And it's not the way to make wheels over and over again , If there is the same function , You can control the code , Definitely in this way first . At this point, you need to call someone else's module component , When you call someone else's module component , Maybe I want to test if it's functional enough . Now I want to use Python abc.py To run the module code , It's a little bit more complicated , First you need to find the directory where the module code is located , In addition, run it in that directory , It will lead to a lot of temporary files , And their own data is not together . Now , Then use Python -m abc Way to run , so Python -m In order to facilitate the testing of library code , Or code to call someone else . Running in this way ,Python The search path is different ,Python abc.py It is required to find the script file under the current directory path , and Python -m abc This is not the way to do it , It will search all the directories for .
First, observe the results intuitively , To write script.py Script , The contents are as follows ：
import sys print(sys.path) print(sys.modules)
Direct execution of scripts
$ python script.py ['/home/ossifrage/workspaces/python-m', '/usr/lib/python36.zip', '/usr/lib/python3.