• <i id='nAE64'><tr id='nAE64'><dt id='nAE64'><q id='nAE64'><span id='nAE64'><b id='nAE64'><form id='nAE64'><ins id='nAE64'></ins><ul id='nAE64'></ul><sub id='nAE64'></sub></form><legend id='nAE64'></legend><bdo id='nAE64'><pre id='nAE64'><center id='nAE64'></center></pre></bdo></b><th id='nAE64'></th></span></q></dt></tr></i><div id='nAE64'><tfoot id='nAE64'></tfoot><dl id='nAE64'><fieldset id='nAE64'></fieldset></dl></div>

      1. <tfoot id='nAE64'></tfoot>

        <legend id='nAE64'><style id='nAE64'><dir id='nAE64'><q id='nAE64'></q></dir></style></legend>
      2. <small id='nAE64'></small><noframes id='nAE64'>

          <bdo id='nAE64'></bdo><ul id='nAE64'></ul>

        configparser 在 Python 3.4 中不工作,NoSectionError 但在 PyCharm 中工作

        configparser not working in Python 3.4, NoSectionError but works fine in PyCharm(configparser 在 Python 3.4 中不工作,NoSectionError 但在 PyCharm 中工作正常)
        <tfoot id='B3RNA'></tfoot>

          <small id='B3RNA'></small><noframes id='B3RNA'>

            • <i id='B3RNA'><tr id='B3RNA'><dt id='B3RNA'><q id='B3RNA'><span id='B3RNA'><b id='B3RNA'><form id='B3RNA'><ins id='B3RNA'></ins><ul id='B3RNA'></ul><sub id='B3RNA'></sub></form><legend id='B3RNA'></legend><bdo id='B3RNA'><pre id='B3RNA'><center id='B3RNA'></center></pre></bdo></b><th id='B3RNA'></th></span></q></dt></tr></i><div id='B3RNA'><tfoot id='B3RNA'></tfoot><dl id='B3RNA'><fieldset id='B3RNA'></fieldset></dl></div>
                <bdo id='B3RNA'></bdo><ul id='B3RNA'></ul>
                  <tbody id='B3RNA'></tbody>

                1. <legend id='B3RNA'><style id='B3RNA'><dir id='B3RNA'><q id='B3RNA'></q></dir></style></legend>
                2. 本文介绍了configparser 在 Python 3.4 中不工作,NoSectionError 但在 PyCharm 中工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 PyCharm 4.5 中编写了 Python 3.4 脚本.(回购:https://github.com/Djidiouf/bbot)在其中,我使用 import configparser 毫无问题地检索 config.cfg 中的某些值:

                  I worked on a Python 3.4 script in PyCharm 4.5. (repo: https://github.com/Djidiouf/bbot ) In it, I used import configparser without any problem for retrieving some values in a 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 版本.

                  Now, I want to deploy it on a Debian 8.1 server. On that server, I have both Python 2.7 and Python 3.4 versions installed.

                  但是当我在机器上运行脚本时,我遇到了以下问题:

                  But when I ran the script on the machine, I have the following issue:

                  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 文件,它根据需要没有 BOM 的 UTF8.我还在机器上用 vi 重新创建了它.

                  I made a lot of research on Internet about that but I didn't find anything valuable. Some people made mention about a corrupt config.cfg file but mine is a LF EOL file with UTF8 without BOM as needed. I also recreated it again with vi on the machine.

                  我还尝试使用 pip 安装 configparser(即使,如果我理解得很好,它现在已内置在 Python 3 中):pip3 install configparser 但是当我这样做时,我有以下错误:

                  I also tried to install configparser with pip (even if, if I understand it well, it's now build-in in Python 3) : pip3 install configparser But when I do that, I have the following error:

                  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 :

                  Here is a config example, I just changed the values and rename the file accordingly to 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 运行脚本.

                  Finally, I just want to say that everything is working on PyCharm 4.5, I just made a git clone, rename the config_example.cfg, change the values, tried to run the script with Python 3.4.

                  推荐答案

                  我发现我在运行脚本时有些不一致.当我在脚本之外的另一个文件夹中时,没有任何效果,但是当我在其中时,它起作用了.可能是因为配置文件是在主脚本和模块中共享和使用的.其实是因为config.read('/some/path/file.cfg')

                  I figured out that I had some inconsistency about running the script. When I was in another folder than my script, nothing worked but when I was in it, it worked. Perhaps because the config file is shared and used in the main script and in a module. In fact it's due to the need to have an absolute path in config.read('/some/path/file.cfg')

                  最后,我找到了答案:https://stackoverflow.com/a/13800583/3301370

                  所以这里是config.cfg在项目根目录下的解决方案/config.cfg:

                  So here is the solution in the case of config.cfg being on the project root directory /config.cfg :

                  /bbot.py

                  import os
                  config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'config.cfg'))
                  

                  但是,由于配置文件在主脚本和子路径模块中共享和使用,因此也必须为子模块设置正确的路径:

                  However, as the config file is shared and used in the main script and in a subpath, module, a correct path must be set for the sub module too:

                  /modules/connection.py

                  import os
                  config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'config.cfg'))
                  

                  它在 PyCharm 中运行良好,因为当您点击脚本的运行按钮时,它会使用项目的路径运行它.事实上,两个 config.read() 语句都有一个相对路径只是因为这个才起作用.

                  It works fine in PyCharm because when you hit the run button for your script, it runs it with the path of the project. The fact that having a relative path for both config.read() statements worked only because of that.

                  使用 pip 安装 configparser 的问题与该特定问题无关,在这种情况下可以忽略,因为 configparser 是 Python3 中的内置库.

                  The problem with installing configparser with pip is unrelated to that specific problem and can be ignore in that case as configparser is a built-in library in Python3.

                  这篇关于configparser 在 Python 3.4 中不工作,NoSectionError 但在 PyCharm 中工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

                      <tfoot id='etSkw'></tfoot>
                      • <bdo id='etSkw'></bdo><ul id='etSkw'></ul>
                          <tbody id='etSkw'></tbody>
                        • <legend id='etSkw'><style id='etSkw'><dir id='etSkw'><q id='etSkw'></q></dir></style></legend>

                            <i id='etSkw'><tr id='etSkw'><dt id='etSkw'><q id='etSkw'><span id='etSkw'><b id='etSkw'><form id='etSkw'><ins id='etSkw'></ins><ul id='etSkw'></ul><sub id='etSkw'></sub></form><legend id='etSkw'></legend><bdo id='etSkw'><pre id='etSkw'><center id='etSkw'></center></pre></bdo></b><th id='etSkw'></th></span></q></dt></tr></i><div id='etSkw'><tfoot id='etSkw'></tfoot><dl id='etSkw'><fieldset id='etSkw'></fieldset></dl></div>

                            <small id='etSkw'></small><noframes id='etSkw'>