python NameError:全局名称'__file__'未定义

问题描述 投票:0回答:15

当我在 python 2.7 中运行此代码时,出现此错误:

Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\pyutilib.subprocess-3.5.4\setup.py", line 30, in <module>
    long_description = read('README.txt'),
  File "C:\Python26\Lib\site-packages\pyutilib.subprocess-3.5.4\setup.py", line 19, in read
    return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
NameError: global name '__file__' is not defined

代码是:

import os
from setuptools import setup


def read(*rnames):
    return open(os.path.join(os.path.dirname(__file__), *rnames)).read()


setup(name="pyutilib.subprocess",
    version='3.5.4',
    maintainer='William E. Hart',
    maintainer_email='[email protected]',
    url = 'https://software.sandia.gov/svn/public/pyutilib/pyutilib.subprocess',
    license = 'BSD',
    platforms = ["any"],
    description = 'PyUtilib utilites for managing subprocesses.',
    long_description = read('README.txt'),
    classifiers = [
        'Development Status :: 4 - Beta',
        'Intended Audience :: End Users/Desktop',
        'License :: OSI Approved :: BSD License',
        'Natural Language :: English',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: Unix',
        'Programming Language :: Python',
        'Programming Language :: Unix Shell',
        'Topic :: Scientific/Engineering :: Mathematics',
        'Topic :: Software Development :: Libraries :: Python Modules'],
      packages=['pyutilib', 'pyutilib.subprocess', 'pyutilib.subprocess.tests'],
      keywords=['utility'],
      namespace_packages=['pyutilib'],
      install_requires=['pyutilib.common', 'pyutilib.services']
      )
python nameerror
15个回答
185
投票

当您在 python 交互式 shell 中附加此行

os.path.join(os.path.dirname(__file__))
时,会出现此错误。

Python Shell
未检测到
__file__
中的当前文件路径,它与您在其中添加此行的
filepath
相关

所以你应该在

os.path.join(os.path.dirname(__file__))
中写下这行
file.py
。然后运行
python file.py
,它可以工作,因为它需要你的文件路径。


40
投票

我在使用 PyInstaller 和 Py2exe 时遇到了同样的问题,所以我在 cx-freeze 的常见问题解答中找到了解决方案。

当从控制台或作为应用程序使用脚本时,下面的函数将为您提供“执行路径”,而不是“实际文件路径”:

print(os.getcwd())
print(sys.argv[0])
print(os.path.dirname(os.path.realpath('__file__')))

来源:
http://cx-freeze.readthedocs.org/en/latest/faq.html

您的旧线路(最初的问题):

def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()

用以下代码片段替换您的代码行。

def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)

使用上面的代码,您可以将应用程序添加到操作系统的路径中,您可以在任何地方执行它,而不会出现应用程序无法找到其数据/配置文件的问题。

用Python测试:

  • 3.3.4
  • 2.7.13

27
投票

我遇到过

__file__
无法按预期工作的情况。但到目前为止,以下内容并没有让我失望:

import inspect
src_file_path = inspect.getfile(lambda: None)

这是最接近 Python 与 C 的类似物

__FILE__

Python 的

__file__
的行为与 C 的
__FILE__
有很大不同。 C 版本将为您提供源文件的原始路径。这对于记录错误和了解哪个源文件有错误非常有用。

Python 的

__file__
只提供当前正在执行的文件的名称,这在日志输出中可能不是很有用。


24
投票

按如下方式更改您的代码!这个对我有用。 `

os.path.dirname(os.path.abspath("__file__"))

15
投票

如果您使用 .py 文件中的代码:使用

os.path.abspath(__file__)
如果您直接在脚本上或在 Jupyter Notebooks 中使用代码: 将 file 放在双引号内。
os.path.abspath("__file__")


10
投票

您使用交互式翻译吗?你可以用

sys.argv[0]

您应该阅读:如何在Python中获取当前执行文件的路径?


9
投票

如果您要查找的只是获取当前工作目录,只要您没有在代码中的其他位置更改工作目录,

os.getcwd()
就会为您提供与
os.path.dirname(__file__)
相同的内容。
os.getcwd()
也可以在交互模式下工作。

所以

os.path.join(os.path.dirname(__file__))
变成
os.path.join(os.getcwd())


7
投票

如果您从 python shell 运行命令,您将得到这个:

>>> __file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined

您需要直接执行该文件,将其作为参数传递给

python
命令:

$ python somefile.py

就你而言,它确实应该是

python setup.py install


3
投票

如果您通过命令行执行文件,则可以使用此技巧

import traceback

def get_this_filename():
    try:
        raise NotImplementedError("No error")
    except Exception as e:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        filename = traceback.extract_tb(exc_traceback)[-1].filename
    return filename

这在 UnrealEnginePython 控制台中对我有用,调用

py.exec myfile.py


1
投票

如果您使用的是 jupyter 笔记本,例如:

MODEL_NAME = os.path.basename(文件)[:-3]

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-f391bbbab00d> in <module>
----> 1 MODEL_NAME = os.path.basename(__file__)[:-3]

NameError: name '__file__' is not defined

你应该放置一个'! ' 前面这样

!MODEL_NAME = os.path.basename(__file__)[:-3]

/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `MODEL_NAME = os.path.basename(__file__)[:-3]'

完成了......


0
投票

我遇到了完全相同的问题,并且可能使用相同的教程。函数定义:

def read(*rnames):
    return open(os.path.join(os.path.dirname(__file__), *rnames)).read()

有问题,因为

os.path.dirname(__file__)
不会返回你需要的东西。尝试将
os.path.dirname(__file__)
替换为
os.path.dirname(os.path.abspath(__file__))
:

def read(*rnames):
    return open(os.path.join(os.path.dirname(os.path.abspath(__file__)), *rnames)).read()

我刚刚向 Andrew 发布了当前文档中的代码片段不起作用的信息,希望它能得到纠正。


0
投票

我认为你可以这样做来获取你的本地文件路径

if not os.path.isdir(f_dir):
    os.mkdirs(f_dir)

try:
    approot = os.path.dirname(os.path.abspath(__file__))
except NameError:
    approot = os.path.dirname(os.path.abspath(sys.argv[1]))
    my_dir= os.path.join(approot, 'f_dir')

0
投票

在 jupyter 笔记本中工作,可以根据您正在处理的脚本方便地设置工作区:

import os
import sys
os.chdir(Path(os.path.realpath('__file__')).parents[0])
sys.path.append(Path(os.path.realpath('__file__')).parents[0])

0
投票

globals()
内置函数可用于测试条件:

if "__file__" in globals():
    path = os.path.join(os.path.dirname(globals()["__file__"])

或者如果

__file__
确实需要设置为某个值:

if "__file__" not in globals():
    __file__ = "/maybe/it/is/here.py"

但是,我不建议这样做,因为它可能会导致交互式会话出现意外结果。


0
投票

也许这可以帮助使用 cx-freeze 的人:

考虑这个构建:

cxfreeze -c app.py --target-dir dist --target-name="app_name.exe" --icon=icon.png

在你的 app.py 标头中考虑以下内容:

import os

DIST_PATH=os.path.dirname(os.path.realpath(__file__))
os.chdir(DIST_PATH)
APP_PATH=os.path.dirname(DIST_PATH)

说明: 可执行文件不会位于 app.py 根文件夹内,而是位于子文件夹“dist”内

© www.soinside.com 2019 - 2024. All rights reserved.