python的__file__和__name__变量
#现在的目录结构为
#现在想要在web2/bin.py中调用web3/main.py模块中的方法

from web3 import main
main.foo() #在pycharm中执行#######
ok #在cmd中执行#######
ModuleNotFoundError: No module named 'web3'

#发现在pycharm中执行与在cmd中执行的结果不同(在cmd中找不到项目目录web1的路径),这是因为pycharm会自动将项目的中路径写入环境变量python中

import sys
print(sys.path) #在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'] #在cmd中执行#######
['E:\9--python\web1\web2', 'D:\python3\python36.zip', 'D:\python3\DLLs','D:\python3\lib', 'D:\python3', 'D:\python3\lib\site-packages']

#==所以如果想要使得在任何环境下代码都可执行的话==,只需手动将项目的路径导入环境变量path中即可,这就需要用到__file__了
#变量__file__表示文件本身,输出的是一个绝对路径(但在pycharm中会自动输出绝对路径),所以需要将其转换成绝对路径

print(__file__) #在pycharm中执行#######
E:/9--python/python_project/web1/web2/bin.py #在cmd中执行#######
bin.py


import sys
import os
DIR_NAME=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(DIR_NAME)
sys.path.append(DIR_NAME) ########
E:\9--python\web1

#现在在cmd中先要从web2/bin.py再调用web3/main.py的方法就可实现了
#所以在pycharm中编程,需要注意其所作的一些优雅操作,防止代码在别的环境下无法执行
#在编程过程中,免不了测试环节,所以现在在web3/main.py中想要测试foo()函数是否能否执行,那么就要在其中调用。但如果现在从web2/bin.py中import模块main.py时,测试所用的foo()方法就会被执行

#在web3/main.py中
def foo():
print('ok')
foo() ########
ok


#在web2/bin.py中
from web3 import main
main.foo() ########
ok
ok

#所以如果想要解决这个问题,就需要__name__变量,现在看看__name__变量在脚本本身执行和在被调用执行的区别

#在web3/main.py中
def bar():
print(__name__)
bar() ########
main


#在web2/bin.py中
from web3 import main
main.bar() ########
web3.main
web3.main

#发现在脚本本身执行的话其输出为__main__,在被调用时执行则输出原脚本的名字
#所以可以在脚本的逻辑代码前加一句判断,使得被调用是不会执行原脚本的逻辑代码,只允许调用方法
if __name__ == '__main__':
逻辑代码
转自:
python的__file__和__name__变量
https://www.cnblogs.com/jianbonet/p/8663040.html
python的__file__和__name__变量的更多相关文章
- python __name__ 变量的含义
python __name__ 变量的含义 if __name__ == '__main__': tf.app.run() 当python读入程序时,会初始化一些系统变量.如果当前程序是主程序,__n ...
- (转)Python学习笔记(1)__name__变量
Python使用缩进对齐组织代码的执行,所有没有缩进的代码,都会在载入时自动执行.每个文件(模块)都可以任意写一些没有缩进的代码,并在载入时自动执行.为了区分 主执行代码和被调用文件,Python引入 ...
- __file__、__name__、__dict__方法整理
本文主要介绍__file__.__name__.__dict__三个方法的作用. #01 __file__:打印当前文件的位置. # import os # print(__file__) # 在py ...
- 扫盲贴000---关于python中的if __name__ == '__main__'
对于python中的__name__变量,根据调用python文件的方式不同,__name__变量的值也会不同.假如我有一个名为hello_world.py的python脚本,当我的目的是直接运行这个 ...
- python解释器遇到if __name__=="__main__"会如何做?
python解释器遇到if __name__=="__main__"会如何做 # Threading example import time, thread def myfunct ...
- Python中的if __name__ == '__main__'
如何简单地理解Python中的if __name__ == '__main__' 1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__ ...
- Python 3 学习笔记之——变量作用域、模块和包
1. 变量作用域 Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python 的作用域一共 ...
- 如何理解python中的if __name__=='main'的作用
一. 一个浅显易懂的比喻 我们在学习python编程时,不可避免的会遇到if __name__=='main'这样的语句,它到底有什么作用呢? <如何简单地理解Python中的if __name ...
- python两个类之间变量和函数的调用
1.class_a() class_b() 2.class_b使用class_a中的变量和函数 3.变量class_a中:class_a.num=... 函数class_b中:先实例化class_a( ...
随机推荐
- context:component-scan报错
文件头补上 xmlns:context=”http://www.springframework.org/schema/context” xsi:schemaLocation=”http://www.s ...
- Vue-cli中使用vConsole,以及设置JS连续点击控制vConsole按钮显隐功能实现
最近发现了一个鹅厂的仓库,实现起来比我这个方便[捂脸].https://github.com/AlloyTeam/AlloyLever 一.vue-cli脚手架中搭建的项目引入vConsole调试 1 ...
- day5:函数练习题
1.写函数,检查获取传入列表或者元祖的对象的所有奇数位索引的元素,并将作为新的列表返回给调用者 #解1: def lis(x): lis_1 = [] for i in range(len(x)): ...
- Redis事务,持久化,哨兵机制
1 Redis事务 基本事务指令 Redis提供了一定的事务支持,可以保证一组操作原子执行不被打断,但是如果执行中出现错误,事务不能回滚,Redis未提供回滚支持. multi 开启事务 exec 执 ...
- 【POJ】2240 Arbitrage
题目链接:http://poj.org/problem?id=2240 题意:n种国家的货币,m个换算汇率.问你能不能赚钱. eg:1美元换0.5英镑,1英镑换10法郎,1法郎换0.21美元,这样1美 ...
- 转Git仓库分支(Branch)和标签(Tag)
仓库的分支(Branch)规范,影响到每个团队的工作流的一致性:标签(Tag)便于开发团队.测 试团队和其他团队识别每个项目的版本,特别是在协同处理线上问题的时候,大家可以非常清楚 地知道线上运行版本 ...
- Deep Dive into Neo4j 3.5 Full Text Search
In this blog we will go over the Full Text Search capabilities available in the latest major release ...
- 7.springboot+mybatis+redis整合
选择生成的依赖 选择保存的工程路径 查询已经生成的依赖,并修改mysql的版本 <dependencies> <dependency> <groupId>org.s ...
- ASCII part of UTF8
UTF8 所以,本着节约的精神,又出现了把Unicode编码转化为“可变长编码”的UTF-8编码. UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1 ...
- 双十一HostGator独立服务器方案
一年一度的“双十一”购物狂欢节到来,各大电商平台线上消费的各种“吸金”开启了“双十一”模式,一年一度的“双十一”网购狂欢又开始以“巨大的价格优势”来勾起消费者的购买欲望. 此次双十一期间,HostGa ...