Python's __file__ and __name__ variables

Source: Internet
Author: User

#现在的目录结构为

To invoke a method in the web3/main.py module #现在想要在web2/bin.py

 from Import Mainmain.foo () # Execute in Pycharm # ####### OK # Execute in CMD # ####### ' web3 '

#发现在pycharm中执行与在cmd中执行的结果不同 (the path to the project directory Web1 is not found in cmd), because Pycharm automatically writes the path of the project to the environment variable in python

ImportSYSPrint(Sys.path)#Execute in Pycharm#######['E:\\9--PYTHON\\WEB1\\WEB2','E:\\9--python\\web1','E:\\9--python\\web1\\venv\\scripts\\python36.zip','D:\\python3\\dlls','D:\\python3\\lib','D:\\python3','e:\\9--python\\web1\\venv','e:\\9--python\\web1\\venv\\lib\\site-packages','E:\\9--python\\web1\\venv\\lib\\site-packages\\setuptools-28.8.0-py3.6.egg','E:\\9--python\\web1\\venv\\lib\\site-packages\\pip-9.0.1-py3.6.egg','d:\\pycharm 2017.3.4\\helpers\\pycharm_matplotlib_backend']#Execute in CMD########['E:\\9--PYTHON\\WEB1\\WEB2','D:\\python3\\python36.zip','D:\\python3\\dlls','D:\\python3\\lib','D:\\python3','d:\\python3\\lib\\site-packages']

#所以如果想要使得在任何环境下代码都可执行的话, just manually import the project's path into the environment variable path, which requires __file__.

#变量__file__表示文件本身, the output is an absolute path (but the absolute path is automatically output in pycharm), so it needs to be converted to an absolute path

 print  (__file__  )  #   execute in Pycharm  #  #######  e:/9--python/python_ Project/web1/web2/bin.py  #   execute  #  #######  bin.py 
Import SYS Import osdir_name=os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)))Print  (dir_name) sys.path.append (dir_name)########E:\9--python\web1

The method of calling web3/main.py #现在在cmd中先要从web2/bin.py can be implemented.

#所以在pycharm中编程, you need to be aware of some of the elegant actions that you can make to prevent code from executing in another environment

#在编程过程中, it is unavoidable to test the link, so now in web3/main.py want to test whether the Foo () function can execute, then it should be called in. However, if you now import the module main.py from web2/bin.py, the Foo () method used for the test will be executed

# in the web3/main.py def foo ():     Print ('ok') foo () # #######OK
# in the web2/bin.py  from Import Mainmain.foo () # ####### Okok

#所以如果想要解决这个问题, you need the __name__ variable, now look at the difference between the __name__ variable being executed in the script itself and the execution being invoked

# in the web3/main.py def Bar ():     Print (__name__) bar () #  __main__
# in the web2/bin.py  from Import Mainmain.bar () # ####### Web3.mainweb3.main

#发现在脚本本身执行的话其输出为__main__, the name of the original script is output when it is invoked

#所以可以在脚本的逻辑代码前加一句判断 so that the invocation is not the logical code that executes the original script, only the method is allowed to be called

if __name__ ' __main__ ' :    Logical Code

Python's __file__ and __name__ variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.