簡體   English   中英

從命令提示符執行 .py 文件引發“ImportError:沒有名為 geopandas 的模塊”- 腳本在 Spyder 中工作(使用 anaconda)

[英]Executing .py file from Command Prompt raises “ImportError: no module named geopandas”- Script works in Spyder (using anaconda)

我有一個完成一些小任務的 python 腳本:

  1. 創建新的目錄結構
  2. 從 URL 下載 a.zip 文件並解壓縮內容
  3. 清理數據
  4. 導出數據為a.csv

full.py 文件在 Spyder 中成功運行並提供所需的 output,但是當嘗試從命令提示符運行.py 時,它會引發“ImportError:沒有名為 geopandas 的模塊”

我正在使用 Windows 10 企業版 1909、conda v4.9.2、Anaconda 命令行客戶端 v 1.7.2、Spyder 4.2.3。

我在一個虛擬環境中,其中包含我的腳本導入的所有需要的包。 我的腳本的第一部分只需要osrequests包,它作為命令提示符中的 own.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 虛擬環境中找不到我安裝的軟件包有關,但我對如何解決這個問題沒有把握。 我以為我之前已將必要的 Anaconda 文件路徑添加到我的 Windows PATH 變量中。

我的虛擬環境包的路徑在“C:\Users\KWOODW01\Anaconda3\envs\pygis\Lib\site-packages”

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

因此,我的pygis venv 包所在的目錄的路徑似乎已經添加到我的 PATH 變量中,但是從命令提示符中,腳本仍然會引發“ImportError:沒有名為 geopandas 的模塊”。 很堅持這個。 希望有人可以提供更多故障排除技巧。 謝謝。

我發現在執行 python 文件之前,我沒有在命令提示符下調用 python。 如果您想從命令提示符執行 a.py 文件,正確的命令是python modulename.py而不是modulename.py 哎呀。 讓這成為其他 python 新手的教訓。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM