Python 从命令提示符下执行.py文件会引发;ImportError:没有名为geopandas的模块";-脚本在Spyder中工作(使用anaconda)

Python 从命令提示符下执行.py文件会引发;ImportError:没有名为geopandas的模块";-脚本在Spyder中工作(使用anaconda),python,anaconda,command-prompt,geopandas,Python,Anaconda,Command Prompt,Geopandas,我有一个python脚本,可以完成一些小任务: 创建新的目录结构 从URL下载.zip文件并解压缩内容 清理数据 将数据导出为.csv格式 在Spyder中时,完整的.py文件会成功运行并提供所需的输出,但在尝试从命令提示符运行.py时,会引发“ImportError:没有名为geopandas的模块” 我使用的是Windows10企业版1909、conda v4.9.2、Anaconda命令行客户端v1.7.2、Spyder 4.2.3 我在一个虚拟环境中,有脚本导入的所有必需的包。 我的脚本

我有一个python脚本,可以完成一些小任务:

  • 创建新的目录结构
  • 从URL下载.zip文件并解压缩内容
  • 清理数据
  • 将数据导出为.csv格式
  • 在Spyder中时,完整的.py文件会成功运行并提供所需的输出,但在尝试从命令提示符运行.py时,会引发“ImportError:没有名为geopandas的模块”

    我使用的是Windows10企业版1909、conda v4.9.2、Anaconda命令行客户端v1.7.2、Spyder 4.2.3

    我在一个虚拟环境中,有脚本导入的所有必需的包。 我的脚本的第一部分只需要
    os
    请求
    包,它可以在命令提示符下作为自己的.py文件正常运行:

    import os
    import requests
    
    #setup folders, download .zip file and unzip it
    
    #working directory is directory the .py file is in
    wd = os.path.dirname(__file__)
    if not os.path.exists(wd):
        os.mkdir(wd)
    #data source directory
    src_path = os.path.join(wd, "src")
    if not os.path.exists(src_path):
        os.mkdir(src_path)
    #data output directory
    output_path = os.path.join(wd,"output")
    if not os.path.exists(output_path):
        os.mkdir(output_path)
    
    #create new output directories and define as variables
    out_parent = os.path.join(wd, "output")
    if not os.path.exists(out_parent):
        os.mkdir(out_parent)
    
    folders = ["imgs", "eruptions_processed"]
    for folder in folders:
        new_dir = os.path.join(out_parent, folder)
        if not os.path.exists(new_dir):
            os.mkdir(new_dir)
        
    output_imgs = os.path.join(out_parent, "imgs")
    if not os.path.exists(output_imgs):
        os.mkdir(output_imgs)
    
    output_eruptions = os.path.join(out_parent, "eruptions_processed")
    if not os.path.exists(output_eruptions):
        os.mkdir(output_eruptions)
    
    
    if not os.path.exists(os.path.join(src_path,"Historical_Significant_Volcanic_Eruption_Locations.zip")):
        
        url = 'https://opendata.arcgis.com/datasets/3ed5925b69db4374aec43a054b444214_6.zip?outSR=%7B%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%7D'
        doc = requests.get(url)
        os.chdir(src_path) #change working directory to src folder
        with open('Historical_Significant_Volcanic_Eruption_Locations.zip', 'wb') as f:
            f.write(doc.content)
        file = os.path.join(src_path,"Historical_Significant_Volcanic_Eruption_Locations.zip") #full file path of downloaded
    
    
    
    但一旦我在.py文件中重新介绍了我的完整包列表:

    import os
    import pandas as pd
    import geopandas as gpd
    import requests
    import datetime
    import shutil
    
    然后从命令提示符下再次运行,我得到:

    Traceback (most recent call last):
      File "C:\Users\KWOODW01\py_command_line_tools\download_eruptions.py", line 17, in <module>
        import geopandas as gpd
    ImportError: No module named geopandas
    

    因此,my
    pygis
    venv软件包所在目录的路径似乎已经添加到我的路径变量中,但在命令提示符下,脚本仍然会引发“ImportError:没有名为geopandas的模块”。这件事很棘手。希望有人能提供更多的故障排除技巧。谢谢。

    在执行python文件之前,我发现我没有在命令提示符下调用python。
    如果要从命令提示符执行.py文件,正确的命令是
    python modulename.py
    ,而不是
    modulename.py
    。哎呀。让这成为其他python新手的一课。

    是否
    pip list
    (或
    conda list
    )list
    geopandas
    (在终端中,激活虚拟环境后)?您好,是的
    conda list
    显示我的虚拟环境中安装了geopandas 0.8.1。因此,如果在终端
    conda activate pygis
    中执行,然后执行
    conda list
    ,它将列出geopandas 0.8.1。在那之后,如果你继续使用python3,然后在Python中执行importgeopandas,是的,我已经完成了所有这些。检查从
    conda列表
    输出安装了
    geopandas
    后,我导航到python文件所在的位置:
    cd py\u command\u line\u tools
    。然后我执行
    下载\u explotions.py
    对不起,你能澄清一下吗?您的意思是,在我激活pygis后,我应该在命令提示符下导入地理命令吗?我不确定我们是否在同一页上。太好了!区别在于,如果您在虚拟环境中使用
    python3 modulename.py
    ,它将在该环境中整洁地运行Python安装,而如果您只运行
    modulename.py
    ,您的操作系统将搜索应用于启动
    .py
    文件的应用程序,这是Python的一般安装,而不是虚拟环境中的安装。非常感谢您的提示!
    C:\Users\KWOODW01\Anaconda3\envs\pygis;C:\Users\KWOODW01\Anaconda3\envs\pygis\Library\mingw-w64\bin;C:\Users\KWOODW01\Anaconda3\envs\pygis\Library\usr\bin;C:\Users\KWOODW01\Anaconda3\envs\pygis\Library\bin;C:\Users\KWOODW01\Anaconda3\envs\pygis\Scripts;C:\Users\KWOODW01\Anaconda3\envs\pygis\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\McAfee\Solidcore\Tools\GatherInfo;C:\Program Files\McAfee\Solidcore\Tools\Scanalyzer;C:\Program Files\McAfee\Solidcore;C:\Program Files\McAfee\Solidcore\Tools\ScGetCerts;C:\Users\KWOODW01\AppData\Local\Microsoft\WindowsApps;C:\Users\KWOODW01\Anaconda3\Library\bin;C:\Users\KWOODW01\Anaconda3\Scripts;C:\Users\KWOODW01\Anaconda3\condabin;C:\Users\KWOODW01\Anaconda3;.