python __file__ is not defined 解决方法

__file__ 是在python module 被导入的时候生成的一个变量,所以在 __file__ 不能被使用,但是又想获取当前文件的路径应该怎么做:

方法一:

import inspect, os.path

filename = inspect.getframeinfo(inspect.currentframe()).filename
path = os.path.dirname(os.path.abspath(filename))

方法二:

import inspect
import os os.path.abspath(inspect.getsourcefile(lambda:0))

参考链接:https://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python/18489147#18489147

python __file__ is not defined 解决方法的更多相关文章

  1. Python更新pip出现错误解决方法

    Python更新pip出现错误解决方法 更新pip python -m pip install --upgrade pip 查看时报错 解决方法 在命令栏(即win+r)输入:easy_install ...

  2. python cmd 窗口 中文乱码 解决方法 (附:打印不同颜色)

    python cmd 窗口 中文乱码 解决方法 (附:打印不同颜色) 前言 在 python 开发中,有时候想通过cmd窗口来和用户交互,比如显示信息之类的,会比自己创建 GUI 来的方便,但是随之而 ...

  3. python socket.error: [Errno 10054] 解决方法

    我用的是python2.7   我搜网上10054错误解决方法的时候发现,大部分文章都是以python3为基础的,对于python2不适用. python socket.error: [Errno 1 ...

  4. python 迭代器 一个奇怪的解决方法

    一般我们在类里面写迭代器都是如下写法: class IterableSomthing: def __iter__(self): return self def __next__(self): retu ...

  5. python 进程内存增长问题, 解决方法和工具

    转载:http://drmingdrmer.github.io/tech/programming/2017/05/06/python-mem.html#pyrasite-%E8%BF%9E%E6%8E ...

  6. Eclipse + Pydev开发Python时import报错解决方法

    一.  原文链接:http://blog.csdn.net/lhanchao/article/details/51306626            用eclipse +PyDev开发python时, ...

  7. windows下Python开发错误记录以及解决方法

    windows下使用pip提示ImportError: cannot import name 'main' 原因:将pip更新为10.0.0后库里面的函数有所变动造成这个问题 解决方法:先卸载现在的p ...

  8. python读文件出现错误解决方法

    python读文件经常会出现 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbd in position 764: illegal multi ...

  9. Python 中文注释报错解决方法

    代码中一旦有了中文注释便会报错. 原因 如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明. 解决方法 在第一行或是第二行加入这么一句# -- coding: utf-8 -- 完美解决

随机推荐

  1. Session服务器之Memcached与Redis

    安装Memcached[root@nginx ~]# yum -y install libevent memcached 指定用户大小等信息,工作环境中常指定大小一般为4到8G,此信息测试使用.[ro ...

  2. TCP为什么要三次握手?

    在<计算机网络>一书中其中有提到,三次握手的目的是“为了防止已经失效的连接请求报文段突然又传到服务端,因而产生错误”,这种情况是: 一端(client)A发出去的第一个连接请求报文并没有丢 ...

  3. Linux 18.04 搭建lamp环境

    Linux 18.04 下搭建lamp环境 一.      安装服务器 a)     在配置好sources.list文件后,apt-get updata&upgrade更新软件: 二.    ...

  4. Had I not seen the Sun(如果我不曾见过太阳)

    Had I not seen the Sun by Emily Dickinson Had I not seen the Sun I could have borne the shade But Li ...

  5. OpenCV.用户选择

    1.Pdf.P153(书.P122) 2. // 来自:"Creating a video with OpenCV — OpenCV 2.4.13.7 documentation.html& ...

  6. Navicate 许可证

    参考: https://blog.csdn.net/weixin_42129270/article/details/81182261

  7. idea的eclipse快速定位link with editor

    eclipse的link with editor,个人感觉非常有用的一个功能 idea也有这个功能autoscroll from source

  8. ajax head带参数两次请求

    ajax请求head里带数据 客户端先发一次option看看能不能登录,然后再发一次post

  9. 【持续更新】一个简洁、易用的美赛 LaTeX 模板: easyMCM

    目录 1 当前美赛模板通行情况的概述 2 EasyMCM 宏包说明 2.1 与 mcmthesis 的关系之说明 2.2 easymcm宏包的简介 2.3 美赛模板下载地址 3 README 摘录 3 ...

  10. [转帖]Linux环境变量设置方法总结 PATH、LD_LIBRARY_PATH

    Linux环境变量设置方法总结 PATH.LD_LIBRARY_PATH 2018年01月17日 21:10:26 晨至曦 阅读数 7548 https://blog.csdn.net/qq_1900 ...