I am trying to figure out what is causing this file called builder.py to not run on mac even though it runs on windows. Code:
import cffi
import glob
import platform
# relative to build dir
LIB_BASE = '../libs/'
# compiling libraries statically to get a single binary
EXTRA_SRC = [LIB_BASE + 'subhook/subhook.c']
pltsysname = {'Windows': 'win32', 'Darwin': 'osx', 'Linux': 'elf'}
pltsrc = pltsysname[platform.system()]
pltsrc = LIB_BASE + 'plthook/plthook_{}.c'.format(pltsrc)
# EXTRA_SRC.append(pltsrc) # disabled until it is actually useful
LIBDIRS = []
if platform.system() == 'Windows':
LIBDIRS.append('../libs/SDL/lib/x86/')
CDEFS = 'generated internals SDL XDL subhook xternPython'.split()
def readfile(name):
with open(name, 'r') as f:
content = f.read()
return content
def build():
ffibuilder = cffi.FFI()
for fname in CDEFS:
ffibuilder.cdef(readfile('cdefs/{}.h'.format(fname)))
ffibuilder.embedding_api('uint32_t kickstart();')
ffibuilder.embedding_init_code(readfile('remote.py'))
ffibuilder.set_source(
'_remote', readfile('cdefs/remote.c'), sources=EXTRA_SRC,
libraries=['SDL2'], library_dirs=LIBDIRS,
define_macros=[('SUBHOOK_STATIC', None)])
ffibuilder.compile(tmpdir='build', target='remote.bin')
if __name__ == '__main__':
build()
When ever I run it I expect it to run but instead it comes up with the following error:
Traceback (most recent call last):
File "/Users/alexanderlee/Desktop/sbpe-1.6.1/builder.py", line 1, in <module>
import cffi
ModuleNotFoundError: No module named 'cffi'
>>>
How do I fix it?
It's probably because you only installed cffi on your Windows, so you probably need to install it on your Mac also.
You can follow rules on the docs:
pip install cffi
cffi is a third-party module. It's installed on your Windows computer but not on your Mac.
My traceback from running pandas takes me to:
site-packages\pandas\io\excel.py line 58, in get_writer
AttributeError: 'module' object has no attribute '__version__'
I found this link to a git issue in the PyInstaller repo
https://github.com/pyinstaller/pyinstaller/issues/1890 and found my openpyxl version, manually added it into the get_writer method like so:
def get_writer(engine_name):
if engine_name == 'openpyxl':
try:
import openpyxl
#remove when conda update available
openpyxl.__version__ = '2.3.2'
# with version-less openpyxl engine
# make sure we make the intelligent choice for the user
if LooseVersion(openpyxl.__version__) < '2.0.0':
return _writers['openpyxl1']
elif LooseVersion(openpyxl.__version__) < '2.2.0':
return _writers['openpyxl20']
else:
return _writers['openpyxl22']
except ImportError:
# fall through to normal exception handling below
pass
try:
return _writers[engine_name]
except KeyError:
raise ValueError("No Excel writer '%s'" % engine_name)
Still no dice. The line number given in the error traceback doesn't even change. I then updated the openpyxl version to 2.3.5, still receiving the error. The openpyxl init file has a version variable in it:
try:
here = os.path.abspath(os.path.dirname(__file__))
src_file = os.path.join(here, ".constants.json")
with open(src_file) as src:
constants = json.load(src)
__author__ = constants['__author__']
__author_email__ = constants["__author_email__"]
__license__ = constants["__license__"]
__maintainer_email__ = constants["__maintainer_email__"]
__url__ = constants["__url__"]
__version__ = constants["__version__"]
except IOError:
# packaged
pass
Any known or potential fixes or workarounds?
Edits were not making an impact because the process was compiled into an exe that these modules were running through. Exported the sections I needed outside of my anaconda environment and now the process works without a hitch.
I will add my workaround to this discussion as I was having the same problem using openpyxl 2.4.0 and maybe a few others are stuck too.
I found that to create a .exe file you have to revert to an older version of openpyxl. To do so:
Open the command prompt and uninstall openpyxl with 'pip uninstall openpyxl'
Reinstall openpyxl using an older version 'pip install openpyxl==2.3.5'
Now you should be able to create your .exe file using py2exe, cx_freeze, etc.
This is my fix: go to your openpyxl site-package folder (for me it's: C:\Python27\Lib\site-packages\openpyxl). Copy all variables in your .constant.json file directly into the _ init _.py file, so it looks like:
import json
import os
__author__= "See AUTHORS"
__author_email__= "eric.gazoni#gmail.com"
__license__= "MIT/Expat",
__maintainer_email__= "openpyxl-users#googlegroups.com"
__url__= "http://openpyxl.readthedocs.org"
__version__= "2.4.0"
try:
here = os.path.abspath(os.path.dirname(__file__))
In clean.py I have:
import datetime
import os
from flask_script import Manager
from sqlalchemy_utils import dependent_objects
from components import db, app
from modules.general.models import File
from modules.workflow import Workflow
manager = Manager(usage='Cleanup manager')
#manager.command
def run(dryrun=False):
for abandoned_workflow in Workflow.query.filter(Workflow.current_endpoint == "upload.upload_init"):
if abandoned_workflow.started + datetime.timedelta(hours=12) < datetime.datetime.utcnow():
print("Removing abandoned workflow {0} in project {1}".format(
abandoned_workflow.id, abandoned_workflow.project.name
))
if not dryrun:
db.session.delete(abandoned_workflow)
db.session.commit()
for file in File.query.all():
dependencies_number = dependent_objects(file).count()
print("File {0} at {1} has {2} dependencies".format(file.name, file.path, dependencies_number))
if not dependencies_number:
file_delete(file, dryrun)
if not dryrun:
db.session.delete(file)
db.session.commit()
# List all files in FILE_STORAGE directory and delete ones tat don't have records in DB
all_files_hash = list(zip(*db.session.query(File.hash).all()))
for file in os.listdir(app.config['FILE_STORAGE']):
if file.endswith('.dat'):
continue
if file not in all_files_hash:
file_delete(os.path.join(app.config['FILE_STORAGE'], file), dryrun)enter code here
I need start def run()
in console I write:
python clean.py
And I have outputs :
`Traceback (most recent call last):
File "cleanup_command.py", line 7, in <module>
from components import db, app
ImportError: No module named 'components'
clean.py is located in- C:\App\model\clean.py
components.py is located in - C:\components.py
Workflow.py is located in - C:\modules\workflow\Workflow.py
Please, tell me what could be the problem?
The problem is that modules for import are searched in certain locations: https://docs.python.org/2/tutorial/modules.html#the-module-search-path.
In your case you can put all source directory paths in PYTHONPATH var like:
PYTHONPATH=... python clean.py
But I guess it would be better to relocate your code files (i.e. put all the libs in one location)
To start run() when you call python clean.py, Add these lines at the end of the script.
if __name__ == '__main__':
r = run()
## 0-127 is a safe return range, and 1 is a standard default error
if r < 0 or r > 127: r = 1
sys.exit(r)
Also as Eugene Primako mentioned it is better to relocate your code files in one location.
from components import db, app
ImportError: No module named 'components'
This means its looking for script named components.py in a location where clean.py is placed. This is the reason why you have got import error.
File setup:
...\Project_Folder
...\Project_Folder\Project.py
...\Project_folder\Script\TestScript.py
I'm attempting to have Project.py import modules from the folder Script based on user input.
Python Version: 3.4.2
Ideally, the script would look something like
q = str(input("Input: "))
from Script import q
However, python does not recognize q as a variable when using import.
I've tried using importlib, however I cannot figure out how to import from the Script folder mentioned above.
import importlib
q = str(input("Input: "))
module = importlib.import_module(q, package=None)
I'm not certain where I would implement the file path.
Repeat of my answer originally posted at How to import a module given the full path?
as this is a Python 3.4 specific question:
This area of Python 3.4 seems to be extremely tortuous to understand, mainly because the documentation doesn't give good examples! This was my attempt using non-deprecated modules. It will import a module given the path to the .py file. I'm using it to load "plugins" at runtime.
def import_module_from_file(full_path_to_module):
"""
Import a module given the full path/filename of the .py file
Python 3.4
"""
module = None
try:
# Get module name and path from full path
module_dir, module_file = os.path.split(full_path_to_module)
module_name, module_ext = os.path.splitext(module_file)
# Get module "spec" from filename
spec = importlib.util.spec_from_file_location(module_name,full_path_to_module)
module = spec.loader.load_module()
except Exception as ec:
# Simple error printing
# Insert "sophisticated" stuff here
print(ec)
finally:
return module
# load module dynamically
path = "<enter your path here>"
module = import_module_from_file(path)
# Now use the module
# e.g. module.myFunction()
I did this by defining the entire import line as a string, formatting the string with q and then using the exec command:
imp = 'from Script import %s' %q
exec imp
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']
)
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 your filepath in which you added this line
So you should write this line os.path.join(os.path.dirname(__file__)) in file.py. and then run python file.py, It works because it takes your filepath.
I had the same problem with PyInstaller and Py2exe so I came across the resolution on the FAQ from cx-freeze.
When using your script from the console or as an application, the functions hereunder will deliver you the "execution path", not the "actual file path":
print(os.getcwd())
print(sys.argv[0])
print(os.path.dirname(os.path.realpath('__file__')))
Source:
http://cx-freeze.readthedocs.org/en/latest/faq.html
Your old line (initial question):
def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
Substitute your line of code with the following snippet.
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)
With the above code you could add your application to the path of your os, you could execute it anywhere without the problem that your app is unable to find it's data/configuration files.
Tested with python:
3.3.4
2.7.13
I've run into cases where __file__ doesn't work as expected. But the following hasn't failed me so far:
import inspect
src_file_path = inspect.getfile(lambda: None)
This is the closest thing to a Python analog to C's __FILE__.
The behavior of Python's __file__ is much different than C's __FILE__. The C version will give you the original path of the source file. This is useful in logging errors and knowing which source file has the bug.
Python's __file__ only gives you the name of the currently executing file, which may not be very useful in log output.
change your codes as follows! it works for me.
`
os.path.dirname(os.path.abspath("__file__"))
If you're using the code inside a .py file: Use
os.path.abspath(__file__)
If you're using the code on a script directly or in Jupyter Notebooks:
Put the file inside double-quotes.
os.path.abspath("__file__")
Are you using the interactive interpreter? You can use
sys.argv[0]
You should read: How do I get the path of the current executed file in Python?
If all you are looking for is to get your current working directory os.getcwd() will give you the same thing as os.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__))
becomes
os.path.join(os.getcwd())
You will get this if you are running the commands from the python shell:
>>> __file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
You need to execute the file directly, by passing it in as an argument to the python command:
$ python somefile.py
In your case, it should really be python setup.py install
If you're exec'ing a file via command line, you can use this hack
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
This worked for me in the UnrealEnginePython console, calling py.exec myfile.py
if you are using jupyter notebook like:
MODEL_NAME = os.path.basename(file)[:-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
you should place a ' ! ' in front like this
!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]'
done.....
I'm having exacty the same problem and using probably the same tutorial. The function definition:
def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
is buggy, since os.path.dirname(__file__) will not return what you need. Try replacing os.path.dirname(__file__) with os.path.dirname(os.path.abspath(__file__)):
def read(*rnames):
return open(os.path.join(os.path.dirname(os.path.abspath(__file__)), *rnames)).read()
I've just posted Andrew that the code snippet in current docs don't work, hopefully, it'll be corrected.
I think you can do this which get your local file path
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')