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. linux netstat 查看网络连接状况

    netstat -lnpnetstat -an |grep 127.0.0.1 tcp 0.0.0.0:* LISTEN tcp 0.0.0.0:* LISTEN [root@wang /]# net ...

  2. 记:linux服务器启动重启WEB项目启动成功,长时间卡住未响应

    问题 云服务器部署web项目,每次正常启动项目后访问页面很久才能有响应,一直卡在INFO: Deploying web application directory ......长达几分钟以上,极度影响 ...

  3. Upgrading CentOS 6 to CentOS 7

    Upgrading CentOS 6 to CentOS 7 November 15th, 2018 — whplus PRE TASKS There are some tasks you can d ...

  4. docker中启动2个mysql实列

    一.mac环境安装docker容器 在docker官网中下载docker容器,地址:https://www.docker.com/products/docker-desktop 具体安装教程及设置网络 ...

  5. DB2部分查询SQL

    /* 部分SQL */ --添加主键 alter TABLE TABLE_SCHEMA.TABLE_NAME add constraint PK_TABLE_NAME primary key(COL1 ...

  6. java http接口请求响应 request response

    接口类: 1 package org.sunshine.dcda.epg.wechat.controller.niao; 2 3 import javax.servlet.http.HttpServl ...

  7. 运算符优先级 以及 && (逻辑与) 和||(逻辑或)的优先级:

    运算符优先级(从高到低列出) 运算符 描述 . [] () 字段访问.数组下标.函数调用以及表达式分组 ++ -- - ~ ! delete new typeof void 一元运算符.返回数据类型. ...

  8. (二)Spring中的ioc

    目录 Spring的ioc操作 ioc底层使用的技术 ioc底层原理 ioc入门案例 bean管理(xml方式) IOC和DI的区别 Spring整合WEB的原理 Spring 整合的具体操作 Spr ...

  9. 超级简单的requests模块教程

    在web后台开发过程中,会遇到需要向第三方发送http请求的场景,python中的requests库可以很好的满足这一要求,这里简要记录一下requests模块的使用! 说明: 这里主要记录一下req ...

  10. Django-djangorestframework-请求模块-获取请求参数

    目录 请求模块 源码分析 正式使用 总结 请求模块 主要是分析 drf 二次封装后的 request 对象 以及怎么拿到请求传递过来的数据(url 拼接的数据,数据包传过来的数据) 源码分析 源码查看 ...