__main__.py usage of Python

Source: Internet
Author: User
Tags python script

Background

Look at flower when you see the __main__.py file, do not know what to do with the specific?

So first test to see.

"Test Code"

The test code directory structure is as follows:

. '--Test    |--__init__.py    |--__main__.py    |--dep.py

__init__.py is empty, indicating that test is a package

__main__.py content is as follows:

[Email protected] ice_test_m 20141024]# more test/__main__.py #!/usr/bin/env python2.7#-*-Coding:utf8-*from __future_ _ Import absolute_importfrom. dep Import DEPCLASSADEP = Depclass ()

  

dep.py content is as follows:

[Email protected] ice_test_m 20141024]# more Test/dep.pyclass Depclass (object):    def __init__ (self):        print " Init Depclass "

  

An error occurred while running __main__.py directly:

[Email protected] ice_test_m 20141024]#/test/__main__.py Traceback (most recent call last):  File "./test/__main__ . Py ", line 6, in <module>    DEP import depclassvalueerror:attempted relative import in Non-package

  

I've met this mistake before.

http://i.cnblogs.com/EditPosts.aspx?postid=4030852

A Python script that contains a relative path import cannot be run directly and can only be referenced as a module.

If you want to run it, you need to do this:

[Email protected] ice_test_m 20141024]# python2.7-m test.__main__    Init depclass

  

__main__.py usage of Python

Related Article

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.