When I run this code in python 2.7, I get this error:
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
code is:
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='wehart@sandia.gov',
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']
)
You will get this if you are running the commands from the python shell:
You need to execute the file directly, by passing it in as an argument to the
python
command:In your case, it should really be
python setup.py install
I solved it by treating file as a string, i.e. put
"__file__"
(together with the quotes!) instead of__file__
This works fine for me:
If you're exec'ing a file via command line, you can use this hack
This worked for me in the UnrealEnginePython console, calling
py.exec myfile.py
I'm having exacty the same problem and using probably the same tutorial. The function definition:
is buggy, since
os.path.dirname(__file__)
will not return what you need. Try replacingos.path.dirname(__file__)
withos.path.dirname(os.path.abspath(__file__))
:I've just posted Andrew that the code snippet in current docs don't work, hopefully, it'll be corrected.
This error comes when you append this line
os.path.join(os.path.dirname(__file__))
in python interactive shell.Python Shell
doesn't detect current file path in__file__
and it's related to yourfilepath
in which you added this lineSo you should write this line
os.path.join(os.path.dirname(__file__))
infile.py
. and then runpython file.py
, It works because it takes your filepath.If all you are looking for is to get your current working directory
os.getcwd()
will give you the same thing asos.path.dirname(__file__)
as long as you have not changed the working directory elsewhere in your code.os.getcwd()
also works in interactive mode.So
os.path.join(os.path.dirname(__file__))
becomesos.path.join(os.getcwd())