Logging module not working with Python3 - python

I am having issues with the standard logging module. If I open a python2.7 shell and import logging everything works fine:
$ python
>>> import logging
>>>
But if I open a python3.4 shell and import logging I get the following error:
$ python3.4
>>> import logging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2222, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 2164, in _find_spec
File "<frozen importlib._bootstrap>", line 1940, in find_spec
File "<frozen importlib._bootstrap>", line 1916, in _get_spec
File "<frozen importlib._bootstrap>", line 1897, in _legacy_get_spec
File "<frozen importlib._bootstrap>", line 863, in spec_from_loader
File "<frozen importlib._bootstrap>", line 904, in spec_from_file_location
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/logging-0.4.9.6-py3.4.egg/logging/__init__.py", line 618
raise NotImplementedError, 'emit must be implemented '\
^
SyntaxError: invalid syntax
I have no idea what the problem is and can't seem to find anyone else who has had the same issue.

logging module is by default there in Python 3 environment .No need to import it.

You seem to have installed a third party library called logging in your Python 3 environment, which is hiding the standard library version, and one of its files has a typo.

I stupidly created a file called logging.py to try out some log features. Then when trying the code below, it is effectively referring to itself and can't find the debug method.
import logging
logging.debug("Debug message")
Changing my file name to logtest.py fixed the problem.

for me it was the previous install of a logging library under an older Python version.
pip3 uninstall logging
fixed it for me.

Related

Error when packaging eel project to an exe

I am building an python project with Eel, and I'm trying to package my program to an exe with PyInstaller as instructed in the documentation. However, I get the following error when I open the .exe:
Error
Traceback (most recent call last):
File "hello.py", line 1, in <module>
import eel
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "eel\__init__.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "bottle.py", line 73, in <module>
AttributeError: 'NoneType' object has no attribute 'write'
I tried to use auto py-to-exe instead of Pyinstaller, but I got the same error. Finally, I tried to package the Eello world example from the Eel documentation to see if it was my program that was the issue, but I still got the same error. I found a similiar problem in a github issue, but I fail to see what I should attach to --add-data.
I am running Python 3.10.8 and Eel 0.14.0. I've now tried python 3.8.9 and python 3.9.13 without luck.
Update
Upon further investigation, I discovered that the error only occours when I attach --noconsole at the end of my cmd python -m eel hello.py web --onefile --noconsole . However, I want my app to work without a visible console.
One of the libraries you are using is attempting to write to sys.stdout and sys.stderr, which are set to None when you run pyinstaller with --windowed option.
You need to explicitly set sys.stderr and sys.stdout in your programs code as early as possible to a writeable object like an open file or an io buffer.
for example:
import sys
outfile = open("logfile.txt", "wt")
sys.stderr = outfile
sys.stdout = outfile

ImportError when debugging but not when running in PyCharm

I am building a desktop app using the python library Kivy. Since I installed a plugin to be able to visualize .kv files, I am not able to debug my code. Yet I am able to run it without problems. This is the error I keep getting:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "...\Worktime_Tool\venv\lib\site-packages\kivy\app.py", line 416, in <module>
from os.path import dirname, join, exists, sep, expanduser, isfile
ImportError: cannot import name 'sep' from 'ntpath' (...\AppData\Local\Programs\Python\Python310\lib\ntpath.py)
So far I do not understand where this ntpath stands for. I tried to compare the ntpath while debugging and while running and they are the same. I also double checked that the interpreter set for the prrject and the interpreter in the run/debug configurations are the same.
I tried with and without the usage of a venv .
Does anyone have an idea why I can run this app but not debug it?
Appreciate much the help!
I had the same problem a while ago. You can either edit the \lib\site-packages\kivy\app.py in your virtual environment to remove the sep from that import line on line 416.
It will look like this after you remove the sep.
from os.path import dirname, join, exists, expanduser, isfile
Or
Update to the latest version of kivy which no longer gives me the error.
You can update to the latest kivy developer build with this command:
python -m pip install "kivy[base] # https://github.com/kivy/kivy/archive/master.zip"

Permission error importing custom functions in Python Anaconda3 Mac OS Catalina

Set-up
After updating to Catalina, Anaconda3 trouble commenced.
I've managed to solve most errors, doing a fresh install as advised by Anaconda, but can't seem to get around a permission error when trying to importing functions from other .py files – something that worked just fine before I upgraded to Catalina.
I run Python 3.7 from Spyder 3.3.6.
Error
Whenever I try to import a custom function, I get,
import os
os.chdir('/Users/mypath/payment_management')
from payment_knab import pay_knab
Traceback (most recent call last):
File "<ipython-input-1-cb99b6800e4e>", line 3, in <module>
from payment_knab import pay_knab
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 906, in _find_spec
File "<frozen importlib._bootstrap_external>", line 1280, in find_spec
File "<frozen importlib._bootstrap_external>", line 1249, in _get_spec
File "<frozen importlib._bootstrap_external>", line 1213, in _path_importer_cache
PermissionError: [Errno 1] Operation not permitted
Question
How do I run Anaconda/Spyder/Python with admin permission? Which command in terminal do I need to execute?
Stupidly enough I have actually managed to run with admin permission before via Terminal, but I forgot how...
I've been looking at StackOverflow for an hour now, but can't find anything that helps.
You've probably forgotten again how to do this. Use sudo spyder, dummy.

PyInstaller error "Python not installed as framework"

I am working on MacOS and I am trying to use PyInstaller to circulate a script to some non tech colleagues. The script is imaginatively called script_V1.py and runs fine.
Up to now, I have had issues with PANDAS and SCIPY but they seem to be resolved with with the command
pyinstaller --onefile --hidden-import pandas._libs.tslibs.timedeltas --hidden-import scipy._lib.messagestream script_V1.py
However, I am now getting a different error (I'll post the full error below)
RuntimeError: Python is not installed as a framework. The Mac OS X
backend will not be able to function correctly if Python is not
installed as a framework. See the Python documentation for more
information on installing Python as a framework on Mac OS X. Please
either reinstall Python as a framework, or try one of the other
backends. If you are using (Ana)Conda please install python.app and
replace the use of 'python' with 'pythonw'. See 'Working with
Matplotlib on OSX' in the Matplotlib FAQ for more information.
I have tried the solution in a similar question on Stack Exchange:
Installation Issue with matplotlib Python
But I think that is actually a different problem with import statements not working within the script itself. In my case if I run the script as a .py script it works fine. Anyway adding the suggested file did not alter the error message. Im new to this and seem to be really struggling with PyInstaller (more than with python itself) so any help much appreciated.
The full error message is (I've added line breaks for clarity)
Traceback (most recent call last):
File "script_V1.py", line 5, in <module>
import matplotlib.pyplot as plt
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/matplotlib/pyplot.py", line 115, in <module>
File "site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/matplotlib/backends/backend_macosx.py", line 17, in <module>
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/loader/pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname)
RuntimeError: Python is not installed as a framework. The Mac OS X
backend will not be able to function correctly if Python is not
installed as a framework. See the Python documentation for more
information on installing Python as a framework on Mac OS X. Please
either reinstall Python as a framework, or try one of the other
backends. If you are using (Ana)Conda please install python.app and
replace the use of 'python' with 'pythonw'. See 'Working with
Matplotlib on OSX' in the Matplotlib FAQ for more information.
[2582] Failed to execute script script_V1
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
So the way I got this to work in the end was to force Matplotlib to use WebAGG and also install the 'tornado' package (which is needed for some reason).

"KeyError: 'PYTHONPATH'". Sites.py broken in python3?

I think this got foobarred when I was doing some brew update stuff. Everytime my system python3 is called, it emits:
Error in sitecustomize; set PYTHONVERBOSE for traceback:
KeyError: 'PYTHONPATH'
This is particularly annoying because I use powerline in my terminal and powerline makes several calls to python each time I execute a command.
Calling Python3 with a the Verbose flag I get:
$ PYTHONVERBOSE=True python3
<multiple pages of python startup info...>
# possible namespace for /usr/local/lib/python2.7/site-packages/backports
# bytecode is stale for 'sitecustomize'
# code object from /usr/local/lib/python2.7/site-packages/sitecustomize.py
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 481, in execsitecustomize
import sitecustomize
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/usr/local/lib/python2.7/site-packages/sitecustomize.py", line 15, in <module>
str(sys.version_info[0]) + '.x!\n PYTHONPATH is currently: "' + str(os.environ['PYTHONPATH']) + '"\n' +
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'PYTHONPATH'
# destroy sitecustomize
import 'site' # <_frozen_importlib_external.SourceFileLoader object at 0x101e3a630>
<more python startup info...>
So the system Python3 is trying to load the Python2 version of sitecustomize? I suspect that's the problem. If so, what are my options for fixing this? brew unlink python3 && brew link python3 didn't help.
I'm not sure what the bytecode is stale for 'sitecustomize' comment python is making is about, but I've also removed the sitecustomize.pyc file, which didn't help.

Categories

Resources