python3--some of the necessary knowledge for collaboration between multiple catalogs

Source: Internet
Author: User
# Auther:aaron Fan

# dynamically gets the relative path path of the execution file:
Print (__file__)

#动态获取执行文件的绝对路径:
Import OS
File_path = Os.path.abspath (__file__)
Print (File_path)

#返回目录名, do not have a file name (return is a path)
Dir_path = Os.path.dirname (File_path)
Print (Dir_path)

#针对刚才的dir_path返回它的上一级目录的绝对路径 (You can do this again and again to get the absolute path to the first-level directory):
Dir_path = Os.path.dirname (Dir_path)
Print (Dir_path)

#添加一个环境变量 (only for this application):
Import Sys
#把刚才生成的那个dir_patch加进去
A = Sys.path.append (Dir_path)
#这种情况下, if there are other directories under the Dir_path directory, you can use import dirname1,dirname2 this way to call the
#也可以直接这么调用这些目中的模块 (the name of the script, do not bring. PY when calling)
#比如from dirname1 Import Main this way
#然后可以通过, Main.test () calls main.py the function or class in this script, etc.

python3--some of the necessary knowledge for collaboration between multiple catalogs

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.