2

从 OSGeo4W 启动的命令窗口开始,我成功安装了与在 windows 7 上运行的 python 2.7 兼容的 fastkml python 包。

当我从命令窗口运行 python 时,我可以从 fastkml 包中导入 kml 模块。

但是,如果我从同一命令窗口启动 IDLE 用户界面并尝试从 fastkml 包中导入 kml 模块,则会收到如下错误:

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
> from fastkml import kml

>Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    from fastkml import kml
  File "C:\OSGeo4W\apps\Python27\lib\site-packages\fastkml\__init__.py", line 28, in <module>
    from pkg_resources import get_distribution, DistributionNotFound
  File "C:\OSGeo4W\apps\Python27\lib\site-packages\pkg_resources\__init__.py", line 77, in <module>
    __import__('pkg_resources.extern.packaging.specifiers')
  File "C:\OSGeo4W\apps\Python27\lib\site-packages\pkg_resources\extern\__init__.py", line 42, in load_module
    __import__(extant)
  File "C:\OSGeo4W\apps\Python27\lib\site-packages\pkg_resources\_vendor\packaging\specifiers.py", line 275, in <module>
    class Specifier(_IndividualSpecifier):
  File "C:\OSGeo4W\apps\Python27\lib\site-packages\pkg_resources\_vendor\packaging\specifiers.py", line 373, in Specifier
    r"^\s*" + _regex_str + r"\s*$", re.VERBOSE | re.IGNORECASE)
  File "C:\OSGeo4W\apps\Python27\lib\re.py", line 190, in compile
    return _compile(pattern, flags)
  File "C:\OSGeo4W\apps\Python27\lib\re.py", line 242, in _compile
    raise error, v # invalid expression
error: nothing to repeat

以下是用于启动 OSgeo4W 的 bat 文件中的代码,而 OSgeo4W 又会启动 IDLE

@echo off
set OSGEO4W_ROOT=%~dp0
rem Convert double backslashes to single
set OSGEO4W_ROOT=%OSGEO4W_ROOT:\\=\%
echo. & echo OSGEO4W home is %OSGEO4W_ROOT% & echo.
set PATH=%OSGEO4W_ROOT%\bin;%PATH%
rem Add application-specific environment settings
for %%f in ("%OSGEO4W_ROOT%\etc\ini\*.bat") do call "%%f"
CALL C:\Python27\Lib\idlelib\idle.bat
@echo on
@if [%1]==[] (echo run o-help for a list of available commands & cmd.exe /k)    else (cmd /c "%*")
4

1 回答 1

1

正如我所怀疑的,您安装了两个 python 2.7。从路径和闪屏中我可以看出:

C:\python27 有 2.7.10,您的批处理文件使用它来运行 IDLE。

C:\OSGeo4W\apps\Python27 必须具有 2.7.4,这可能是您直接运行的 python,并且可以正确导入。

在两个 shell 中运行>>> import sys; sys.executable将验证或更正这些结论。如果它们是正确的,那么可能的解决方法是C:\python27在批处理文件中替换为,C:\OSGeo4W\apps\Python27以便 IDLE 在有效的 python 上运行。当然,也有可能C:\OSGeo4W\apps\Python27\Lib不包含\idlelib.

于 2017-02-11T03:27:40.773 回答