Python, import, module
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的更多相关文章
- 测试linux python import module
源码test.py #!/usr/bin/env python # -*- coding: UTF-8 -*- import os os.system("df -h") 运行结果( ...
- python import, from xx import yy
区别: 用import modulexx/packagexx.moduleyy是导入某一模块,如果想引用模块的内容(class, method,variables...)必须用全名,即 [module ...
- Python: import vs from (module) import function(class) 的理解
Python: Import vs From (module) import function(class) 本文涉及的 Python 基本概念: Module Class import from . ...
- python import 错误 TypeError: 'module' object is not callable
python import 错误 TypeError: 'module' object is not callable 在这里,有 Person.py test.py; 在 test.py 里面 im ...
- python import eventlet包时提示ImportError: cannot import name eventlet
root@zte-desktop:/home/ubuntu/python-threads# cat eventlet.py #!/usr/bin python import eventlet from ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- python import
在执行 import module 时 会从 1 当前目录 2 pythonpath(可以通过 os.sys.path 查看) 3 python 安装目录 b import 了 a, c impo ...
- Python import / pyd / dll
使用Python import 模块时, 先会在模块的搜索path里依次搜索(前面会覆盖之后出现的同名模块),次序为: 1. 程序的主目录(交互模式下当前的工作目录或 脚本文件所在的目录) 2. 环境 ...
- Python import与from import使用及区别介绍
Python程序可以调用一组基本的函数(即内建函数),比如print().input()和len()等函数.接下来通过本文给大家介绍Python import与from import使用及区别介绍,感 ...
随机推荐
- ThinkPHP5——route(路由)的详解
路由在框架中的作用打个比方的话,路由好比是WEB应用的总调度室,对于访问的URL地址,路由可以拒绝或者接受某个URL请求,并进行分发调度,而且还有一个副作用是因为路由规则可以随意定义,因此可以让你的U ...
- 微信小程序——动态修改页面数据(和样式)及参数传递
1.1.1动态修改页面数据 在小程序中我们经常要动态渲染数据,对于新手而言我们常常遇到修改的数据在控制台显示和页面显示不一致,因为我们用“=”修改数据的,这种是可以修改,但无法改变页面的状态的,还会造 ...
- 【Python成长之路】装逼的一行代码:快速共享文件【华为云分享】
[写在前面] 有时候会与同事共享文件,正常人的操作是鼠标右键,点击共享.其实有个装逼的方法,用python的一行代码快速实现基于http服务的共享方式. [效果如下] [示例代码] 在cmd窗口进入想 ...
- OSS 对象存储的那些事AmazonS3简单使用
对象存储是根据AmazonS3来做的封装,主要功能 :文件的上传下载 生成链接 对图片的处理 查看桶内对象等一系列的操作. 本文主要做的是文件的上传下载生成链接以及前期的准备工作 以springboo ...
- atom常用快捷键-mac亲测
目录 最常用 其他 测试没成功 最常用 中文 快捷键 功能 新建界面窗口 Ctrl + Shift + N 新建文件 Ctrl + N 打开文件 Ctrl + O 打开文件夹 Ctrl + Shift ...
- CodeForces-Round235D
链接:http://codeforces.com/contest/401/problem/D 题意:给出一个数字num和m,问通过重新排列num中的各位数字中有多少个数(mod m)=0,直接枚举 ...
- B.Beautiful Numbers
题意:你被给予了一个序列 p = [p1, p2, ..., pn](1 ~ n的整数),如果存在l, r左右端点(1 <= l <= r <= n),使得[pl, pl+1,... ...
- windows安装apache+mysql+php
文件打包下载,包括apache.mysql.php,地址如下: 链接: https://pan.baidu.com/s/1Mcm4OxJV45UWsktBycw7mQ 密码: dwy6 安装apach ...
- tensorflow学习笔记——VGGNet
2014年,牛津大学计算机视觉组(Visual Geometry Group)和 Google DeepMind 公司的研究员一起研发了新的深度卷积神经网络:VGGNet ,并取得了ILSVRC201 ...
- wxxcx_learn
32个字符组成的一组随即字符串 function getRandChar($length){ $str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWX ...