When the interpreter reads a python script file, it does two things:

(1) set some special variable.

(2) it executes all the code from 1st line of that script file.

__name__ (2 underscores before and after) is a special python variable.

we can import this script as a module.

and also execute this script directly that the interpreter will assign the hard-coded string "__main__" to the __name__ variable

# foo.py
# I am using python 3.4

print ("Befor foo function.")
def foo():
print ("foo function.")
print ("After foo function.")
if __name__ == "__main__":
foo()

if we run it with

$ python foo.py

then the result is :

Befor foo function.
After foo function.
foo function.

and if we import it, then run $ python hello.py

# hello.py
import foo

the running result is:

Befor foo function.
After foo function.

Python, import, module的更多相关文章

  1. 测试linux python import module

    源码test.py #!/usr/bin/env python # -*- coding: UTF-8 -*- import os os.system("df -h") 运行结果( ...

  2. python import, from xx import yy

    区别: 用import modulexx/packagexx.moduleyy是导入某一模块,如果想引用模块的内容(class, method,variables...)必须用全名,即 [module ...

  3. Python: import vs from (module) import function(class) 的理解

    Python: Import vs From (module) import function(class) 本文涉及的 Python 基本概念: Module Class import from . ...

  4. python import 错误 TypeError: 'module' object is not callable

    python import 错误 TypeError: 'module' object is not callable 在这里,有 Person.py test.py; 在 test.py 里面 im ...

  5. python import eventlet包时提示ImportError: cannot import name eventlet

    root@zte-desktop:/home/ubuntu/python-threads# cat eventlet.py #!/usr/bin python import eventlet from ...

  6. Requests:Python HTTP Module学习笔记(一)(转)

    Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...

  7. python import

    在执行 import module 时 会从 1 当前目录 2 pythonpath(可以通过 os.sys.path 查看) 3 python 安装目录   b import 了 a, c impo ...

  8. Python import / pyd / dll

    使用Python import 模块时, 先会在模块的搜索path里依次搜索(前面会覆盖之后出现的同名模块),次序为: 1. 程序的主目录(交互模式下当前的工作目录或 脚本文件所在的目录) 2. 环境 ...

  9. Python import与from import使用及区别介绍

    Python程序可以调用一组基本的函数(即内建函数),比如print().input()和len()等函数.接下来通过本文给大家介绍Python import与from import使用及区别介绍,感 ...

随机推荐

  1. Qt事件分发机制源码分析之QApplication对象构建过程

    我们在新建一个Qt GUI项目时,main函数里会生成类似下面的代码: int main(int argc, char *argv[]) { QApplication application(argc ...

  2. SQL 数字转为中文大写

    USE [SPECIAL_BLD]GO SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO CREATE FUNCTION [dbo].[get_upper] ...

  3. 使用Docker测试静态网站

    参考书籍 :第一本docker书[澳]James Turnbull  1.Sample网站的初始Dockerfile 文件目录如下: Dockerfile文件代码: 安装nginx 在容器中创建一个目 ...

  4. luogu P3807 【模板】卢卡斯定理

    求 C(n,n+m)%p C(m,n)%p=C(m%p,n%p)*C(m/p,n/p) #include<cstdio> #include<cstring> #include& ...

  5. Pandas里面常用的一些数据分析函数总结

    import pandas as pdimport numpy as np pandas 有两个主要的数据结构:Series 和 DataFrame:Series 是一个一维数组对象 ,它包含一组索引 ...

  6. python列表式推导

    1.基本语法 [表达式 for 变量 in 列表] 或者  : [表达式 for 变量 in 列表 if 条件] 2.示例 生成列表 li=[x for x in range(10)] print(l ...

  7. RequireJS 打包工具

    r.js是RequireJS的一个附产品,支持在 NodeJS环境下运行AMD程序,并且其包含了一个名为RequireJS Optimizer的工具,可以为项目完成合并脚本等优化操作 RequireJ ...

  8. 【算法】272-每周一练 之 数据结构与算法(Dictionary 和 HashTable)

    这是第五周的练习题,上周忘记发啦,这周是复习 Dictionary 和 HashTable. 下面是之前分享的链接: [算法]200-每周一练 之 数据结构与算法(Stack) [算法]213-每周一 ...

  9. .Net Core 3.0 以及其前版本编写自定义主机,以允许本机程式(转载)

    像所有的托管代码一样,.NET Core 应用程序也由主机执行. 主机负责启动运行时(包括 JIT 和垃圾回收器等组件)和调用托管的入口点. 托管 .NET Core 运行时是高级方案,在大多数情况下 ...

  10. Nginx(一)--nginx的初步认识及配置

    什么是Nginx 是一个高性能的反向代理服务器正向代理代理的是客户端反向代理代理的是服务端 Apache.Tomcat.Nginx 静态web服务器jsp/servlet服务器 tomcat 安装Ng ...