I'm new to logging in Python and have tried to build a basic logger that writes to a file. The problem I have is that the file is not created, yet there are not any errors thrown. Any ideas?
Using Spyder IDE in Anaconda (in case that is applicable)
Code:
import pandas as pd
import logging
format = "%(asctime)s %(message)s"
logging.basicConfig(format=format, level=logging.DEBUG, filename='H://logfile.log')
now = pd.datetime.now()
logging.info("Time Created")
I'm using python 2.7.11 on osx, I reproduced your steps as also explained here
import logging
format = "%(asctime)s %(message)s"
logging.basicConfig(format=format, level=logging.DEBUG, filename='logfile.log')
logging.info("Hello World")
A file named logfile.log is created in directory from which I executed python.
You can check the dir in which python is running typing:
pwd
My guess is that there is a problem with the permission of the folder in which you want to create the log file, or in the way you are writing the absolute path of the folder.
I would advise you to create first a log file in the local dir ( which is the output of the pwd command ) and see if this works.
Appears the problem is with Spyder IDE v. 2.3.8 (maybe mine is outdated). The log file is created in command prompt but in Spyder the log file isn't created.
Related
I'm trying to debug a project that has a lot of additional libraries added to PYTHONPATH at runtime before launching the python file.
I was not able to add those commands with tasks.json file prior to debugging python file in Visual Studio code (see post Visual Studio Code unable to set env variable paths prior to debugging python file), so I'm just adding them via an os.system("..") command
I'm only showing 1 of the libraries added below:
# Standard library imports
import os
import sys
os.system("SET PYTHONPATH=D:\\project\\calibration\\pylibrary\\camera")
# Pylibrary imports
from camera import capture
When I debug, it fails on line from camera import capture with:
Exception has occurred: ModuleNotFoundError
No module named 'camera'
File "D:\project\main.py", line 12, in <module>
from camera.capture import capture
I also tried
os.environ['PYTHONPATH']="D:\\project\\pylibrary\\camera" and I still get the same error
Why is it not remembering the pythonpath while running the script?
How else can I define the pythonpath while running Visual Studio Code and debugging the project file?
I know I can add the pythonpath to env variables in windows, but it loads too many libraries and I want it to only remember the path while the python script is executed.
Thanks
Using os.system() won't work because it starts a new cmd.exe shell and sets the env var in that shell. That won't affect the env vars of the python process. Assigning to os.environ['PYTHONPATH'] won't work because at that point your python process has already cached the value, if any, of that env var in the sys.path variable. The solution is to
import sys
sys.path.append(r"D:\project\calibration\pylibrary\camera")
I'm building an app with PyQt5.
PyQt5 = 5.6.0
Python = 3.4.5
IDE = PyCharm Community Edition 2018 EAP
OS = Windows 10
The trouble I have is that any print()s I have in my code don't show up in the console when I run the app. This is making debugging very difficult as you can expect. For example,
module1.py (no PyQt/GUI code here): If I run this module separately then all the prints() are output to console
module2.py (all PyQt/GUI code is here): If I run this (it calls functions in module1.py) then the prints() do not output to console.
Someone suggested a solution here: https://forums.autodesk.com/t5/motionbuilder-forum/pyqt-pyside-event-handlers-don-t-print-to-console/td-p/7058029 but the module named pythonidelib doesn't seem to exist. I checked in Anaconda and other places but could not find it.
Does anyone know how to make print() work within a PyQt5 application or if there is an alternative.
try python logging,
like this:
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
logging.debug("print message!!!")
In version 4.1.0:
import logging
r = logging.getLogger()
r.setLevel(logging.DEBUG)
logging.debug("debug")
will log into the console/terminal.
And we can get a default StreamHandler by:
stream_handler = root.handlers[0]
But in 4.1.1, the handler is missing and the code above will log to the webpage.
I can not find the release note or changelog of 4.1.1.
How can I log into console in the latest version of jupyter notebook?
The solution is add the standard output unit by myself.
root = logging.getLogger()
root.addHandler(logging.StreamHandler(os.fdopen(1, "w")))
now, logging.debug log into the console
we´re having python source code stored in a sql database, the code is build together to a virtual python module and can be executed.
We want to debug this modules but then of course the Eclipse debugger host doesnt know where to find the source code for these modules.
Is there a way to provide pydevd with the location of the source code, even if that means to write down the files to disk?
Write it to the disk and when doing the compile pass the filename for the code (and, when you're not in debug mode, just don't write it and pass '<string>' as the filename).
See the example below:
from tempfile import mktemp
my_code = '''
a = 10
print a
'''
tmp_filename = mktemp('.py', 'temp_file_')
with open(tmp_filename, 'w') as f:
f.write(my_code)
obj = compile(my_code, tmp_filename, 'exec')
exec obj #Place breakpoint here: when stepping in it should get to the code.
You need to add module to PYTHONPATH in Eclipse project settings and import it using the standard Python import. Then PyDev debugger should find it without any problems.
I've got a Django product I'm using iPython to interact with.
I'm trying to have modules automatically loaded when I start a shell:
python manage.py shell
I've copied .ipython/ipythonrc to the root directory of the project and added to the file:
import_some module_name model1 model2
However, when I start the shell, these names are not being loaded.
What am I doing wrong?
I don't know about ipythonrc, but if you only need the models, you could use django-extensions. After you install it, you've got a plethora of new managment commands, including shell_plus, which will open a ipython session and autoload all your models:
python manage.py shell_plus
BryanWheelock Your solution won't work because your shell is the result of the spawn not a direct interatction with it. What you want to do is this - or at least this is what I do.
Within your workspace (the place where you type python manage.py shell) create a ipythonrc file. In it put the following:
include ~/.ipython/ipythonrc
execute from django.contrib.auth.models import User
# .
# .
# .
execute import_some module_name model1 model2
For example I also add the following lines in mine..
# Setup Logging
execute import sys
execute import logging
execute loglevel = logging.DEBUG
execute logging.basicConfig(format="%(levelname)-8s %(asctime)s %(name)s %(message)s", datefmt='%m/%d/%y %H:%M:%S', stream=sys.stdout )
execute log = logging.getLogger("")
execute log.setLevel(loglevel)
execute log.debug("Logging has been initialized from ipythonrc")
execute log.debug("Root Logger has been established - use \"log.LEVEL(MSG)\"")
execute log.setLevel(loglevel)
execute log.debug("log.setlevel(logging.DEBUG)")
execute print ""
This allows you to use logging in your modules and keep it DRY. Hope this helps.
shell_plus command of django-extensions can import the model automatically, but it seems can not load the profile of ipython. I have did some hacky job to make this done.
use start_ipython to launch ipython shell instead of embed and pass some arguments to it.
I have also wrote a blog post, you can find the detail here