我在PyCharm 4.5中使用Python 3.4脚本。 (回复:https://github.com/Djidiouf/bbot)
在其中,我使用import configparser
在config.cfg中检索一些值没有任何问题:
config = configparser.RawConfigParser()
config.read('config.cfg')
server = config.get('bot_configuration', 'server')
channel = config.get('bot_configuration', 'channel')
botnick = config.get('bot_configuration', 'botnick')
port = config.getint('bot_configuration', 'port')
现在,我想将它部署在Debian 8.1服务器上。在那台服务器上,我安装了Python 2.7和Python 3.4版本。
但是当我在机器上运行脚本时,我遇到了以下问题:
djidiouf@linuxserver:/home/djidiouf# /usr/bin/python3.4 /home/djidiouf/bbot/bbot.py
Traceback (most recent call last):
File "/usr/lib/python3.4/configparser.py", line 1116, in _unify_values
sectiondict = self._sections[section]
KeyError: 'bot_configuration'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/djidiouf/bbot/bbot.py", line 25, in <module>
import modules.steam # Contains specific Steam-Valve related functions
File "/home/djidiouf/bbot/modules/steam.py", line 13, in <module>
import modules.connection
File "/home/djidiouf/bbot/modules/connection.py", line 37, in <module>
server = config.get('bot_configuration', 'server')
File "/usr/lib/python3.4/configparser.py", line 754, in get
d = self._unify_values(section, vars)
File "/usr/lib/python3.4/configparser.py", line 1119, in _unify_values
raise NoSectionError(section)
configparser.NoSectionError: No section: 'bot_configuration'
我在互联网上做了很多研究,但我找不到任何有价值的东西。有些人提到了一个损坏的config.cfg文件,但我的是一个LF EOL文件,UTF8没有BOM,根据需要。我还在机器上用vi重新创建了它。
我还尝试用pip安装configparser(即使我理解得很好,现在它已经在Python 3中内置了):pip3 install configparser
但是当我这样做时,我有以下错误:
djidiouf@linuxserver:/home/djidiouf# sudo pip3 install -v configparser
[sudo] password for djidiouf:
Downloading/unpacking configparser
Ignoring link https://pypi.python.org/packages/source/c/configparser/configparser-3.5.0b1.tar.gz#md5=d60ca2c714acb4adaf5818c6a1ffd78b (from https://pypi.python.org/simple/configparser/), version 3.5.0b1 is a pre-release (use --pre to allow).
Ignoring link https://pypi.python.org/packages/source/c/configparser/configparser-3.5.0b2.tar.gz#md5=ad2a71db8bd9a017ed4735eac7acfa07 (from https://pypi.python.org/simple/configparser/), version 3.5.0b2 is a pre-release (use --pre to allow).
Using version 3.3.0r2 (newest of versions: 3.3.0r2, 3.3.0r1, 3.2.0r3, 3.2.0r2, 3.2.0r1)
Downloading configparser-3.3.0r2.tar.gz
Downloading from URL https://pypi.python.org/packages/source/c/configparser/configparser-3.3.0r2.tar.gz#md5=dda0e6a43e9d8767b36d10f1e6770f09 (from https://pypi.python.org/simple/configparser/)
Running setup.py (path:/tmp/pip-build-v7av3db7/configparser/setup.py) egg_info for package configparser
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 5, in <module>
import distutils.core
File "/usr/lib/python3.4/distutils/core.py", line 18, in <module>
from distutils.config import PyPIRCCommand
File "/usr/lib/python3.4/distutils/config.py", line 7, in <module>
from configparser import ConfigParser
File "/tmp/pip-build-v7av3db7/configparser/configparser.py", line 397
_KEYCRE = re.compile(ur"%\(([^)]+)\)s")
^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 5, in <module>
import distutils.core
File "/usr/lib/python3.4/distutils/core.py", line 18, in <module>
from distutils.config import PyPIRCCommand
File "/usr/lib/python3.4/distutils/config.py", line 7, in <module>
from configparser import ConfigParser
File "/tmp/pip-build-v7av3db7/configparser/configparser.py", line 397
_KEYCRE = re.compile(ur"%\(([^)]+)\)s")
^
SyntaxError: invalid syntax
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-v7av3db7/configparser
Exception information:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 290, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/usr/lib/python3/dist-packages/pip/req.py", line 1230, in prepare_files
req_to_install.run_egg_info()
File "/usr/lib/python3/dist-packages/pip/req.py", line 326, in run_egg_info
command_desc='python setup.py egg_info')
File "/usr/lib/python3/dist-packages/pip/util.py", line 716, in call_subprocess
% (command_desc, proc.returncode, cwd))
pip.exceptions.InstallationError: Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-v7av3db7/configparser
Storing debug log for failure in /root/.pip/pip.log
这是一个配置示例,我只是更改了值并相应地将文件重命名为config.cfg:
[bot_configuration]
server = your.irc.server.com
channel = ##your_channel
botnick = bbot
port = 6667
admins = You,Him
https://github.com/Djidiouf/bbot/blob/master/config_example.cfg
最后,我只想说一切都在PyCharm 4.5上工作,我只做了一个git clone
,重命名了config_example.cfg,更改了值,尝试用Python 3.4运行脚本。
答案 0 :(得分:2)
安装错误来自_KEYCRE = re.compile(ur"%\(([^)]+)\)s")
,这导致语法错误,因为python3不支持ur
前缀,pip正在尝试安装python2包。
您的第一个错误是keyError 'bot_configuration'
,这不是因为没有安装configparser,configparser
是标准库的一部分。
bot_configuration
KeyError
是因为python无法找到您的配置文件,如果您在pycharm中使用config.read('foobar')
,您将看到完全相同的输出
答案 1 :(得分:0)
我发现我在运行脚本方面存在一些不一致。当我在另一个文件夹而不是我的脚本时,没有任何工作,但当我在其中,它工作。也许是因为配置文件在主脚本和模块中共享和使用。
事实上,这是由于需要在config.read('/some/path/file.cfg')
最后,我找到答案:https://stackoverflow.com/a/13800583/3301370
所以这是config.cfg在项目根目录 /config.cfg 上的解决方案:
/bbot.py
import os
config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'config.cfg'))
但是,由于配置文件在主脚本和子路径模块中共享和使用,因此必须为子模块设置正确的路径:
/modules/connection.py
import os
config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'config.cfg'))
它在PyCharm中运行正常,因为当您点击脚本的运行按钮时,它会使用项目的路径运行它。事实上,两个config.read()
语句的相对路径只能因此而起作用。
使用pip安装configparser的问题与该特定问题无关,在这种情况下可以忽略,因为configparser是Python3中的内置库。