我遇到了标准日志记录模块的问题。如果我打开一个python2.7 shell并导入日志记录,一切正常:
$ python
>>> import logging
>>>
但是如果我打开python3.4 shell并导入日志记录,我会收到以下错误:
$ 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
我不知道问题是什么,似乎找不到其他有同样问题的人。
答案 0 :(得分:11)
您似乎在Python 3环境中安装了名为private PrintDocument docToPrint;
private string stringToPrint;
public mainForm()
{
InitializeComponent();
CenterToScreen();
this.docToPrint = new PrintDocument();
(...)
}
private void tsBtnPrint_Click(object sender, EventArgs e)
{
PrintDialog myPrintDialog = new PrintDialog();
myPrintDialog.AllowCurrentPage = true;
myPrintDialog.AllowSelection = true;
myPrintDialog.AllowSomePages = true;
myPrintDialog.Document = docToPrint;
if(myPrintDialog.ShowDialog()==DialogResult.OK)
{
StringReader reader = new StringReader(this.richTextBox.Text);
stringToPrint = reader.ReadToEnd();
this.docToPrint.PrintPage += new PrintPageEventHandler(this.docToPrintCustom);
this.docToPrint.Print();
}
}
private void docToPrintCustom(object sender, PrintPageEventArgs e)
{
Font PrintFont = this.richTextBox.Font;
SolidBrush PrintBrush = new SolidBrush(Color.Black);
int LinesPerPage = 0;
int charactersOnPage = 0;
e.Graphics.MeasureString(stringToPrint, PrintFont, e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out LinesPerPage);
e.Graphics.DrawString(stringToPrint, PrintFont, PrintBrush, e.MarginBounds, StringFormat.GenericTypographic);
stringToPrint = stringToPrint.Substring(charactersOnPage);
MessageBox.Show(stringToPrint.Length.ToString());
e.HasMorePages = (stringToPrint.Length > 0);
PrintBrush.Dispose();
}
的第三方库,该库隐藏了标准库版本,其中一个文件存在拼写错误。
答案 1 :(得分:9)
日志模块默认存在于Python 3环境中。不需要导入它。
答案 2 :(得分:2)
我愚蠢地创建了一个名为 logging.py 的文件,以尝试一些日志功能。然后,在尝试下面的代码时,它实际上是在引用自身,并且找不到调试方法。
import logging
logging.debug("Debug message")
将我的文件名更改为 logtest.py 即可解决问题。
答案 3 :(得分:0)
对我来说,这是以前在旧 Python 版本下安装的日志库。
pip3 uninstall logging
帮我修好了。