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

2024-10-03 00:17:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  1. 创建新的目录结构
  2. 从URL下载.zip文件并解压缩内容
  3. 清理数据
  4. 将数据导出为.csv格式

在Spyder中时,完整的.py文件会成功运行并提供所需的输出,但在尝试从命令提示符运行.py时,会引发“ImportError:没有名为geopandas的模块”

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

我在一个虚拟环境中,有脚本导入的所有必需的包。 脚本的第一部分只需要osrequests包,并且在命令提示符下作为自己的.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

我认为这个问题与在我的anaconda虚拟环境中找不到我安装的软件包有关,但我对如何解决这个问题没有很好的把握。我以为我以前已经在Windows路径变量中添加了必要的Anaconda文件路径

虚拟环境包的路径位于中 “C:\Users\KWOODW01\Anaconda3\envs\pygis\Lib\site包”

echo %PATH% 返回:

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;.

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


Tags: pathpyimportsrcoutputifosexists
1条回答
网友
1楼 · 发布于 2024-10-03 00:17:19

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

相关问题 更多 >