简体   繁体   English

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

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

I have a python script that accomplishes a few small tasks:我有一个完成一些小任务的 python 脚本:

  1. Create new directory structure创建新的目录结构
  2. Download a.zip file from a URL and unzip contents从 URL 下载 a.zip 文件并解压缩内容
  3. Clean up the data清理数据
  4. Export data as a.csv导出数据为a.csv

The full.py file runs successfully giving desired output when in Spyder, but when trying to run the.py from Command Prompt, it raises "ImportError: no module named geopandas" full.py 文件在 Spyder 中成功运行并提供所需的 output,但是当尝试从命令提示符运行.py 时,它会引发“ImportError:没有名为 geopandas 的模块”

I am using Windows 10 Enterprise version 1909, conda v4.9.2, Anaconda command line client v 1.7.2, Spyder 4.2.3.我正在使用 Windows 10 企业版 1909、conda v4.9.2、Anaconda 命令行客户端 v 1.7.2、Spyder 4.2.3。

I am in a virtual environment with all the needed packages that my script imports.我在一个虚拟环境中,其中包含我的脚本导入的所有需要的包。 The first part of my script only needs os and requests packages, and it runs fine as its own.py file from Command Prompt:我的脚本的第一部分只需要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


But once I re-introduce my full list of packages in the.py file:但是一旦我在 .py 文件中重新引入我的完整包列表:

import os
import pandas as pd
import geopandas as gpd
import requests
import datetime
import shutil

and run again from Command Prompt, I get:并从命令提示符再次运行,我得到:

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

I am thinking the problem is something to do with not finding my installed packages in my anaconda virtual environment, but I don't have a firm grasp on how to troubleshoot that.我认为这个问题与在我的 anaconda 虚拟环境中找不到我安装的软件包有关,但我对如何解决这个问题没有把握。 I thought I had added the necessary Anaconda file paths to my Windows PATH variable before.我以为我之前已将必要的 Anaconda 文件路径添加到我的 Windows PATH 变量中。

The path to my virtual environment packages are in "C:\Users\KWOODW01\Anaconda3\envs\pygis\Lib\site-packages"我的虚拟环境包的路径在“C:\Users\KWOODW01\Anaconda3\envs\pygis\Lib\site-packages”

echo %PATH% returns: 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;.

So it appears that the path to the directory where my pygis venv packages live are already added to my PATH variables, yet from Command Prompt the script still raises the "ImportError: no module named geopandas".因此,我的pygis venv 包所在的目录的路径似乎已经添加到我的 PATH 变量中,但是从命令提示符中,脚本仍然会引发“ImportError:没有名为 geopandas 的模块”。 Pretty stuck on this one.很坚持这个。 Hoping someone can provide some more troubleshooting tips.希望有人可以提供更多故障排除技巧。 Thanks.谢谢。

I figured out I wasn't calling python in command prompt before executing the python file.我发现在执行 python 文件之前,我没有在命令提示符下调用 python。 The proper command is python modulename.py instead of modulename.py if you want to execute a.py file from the command prompt.如果您想从命令提示符执行 a.py 文件,正确的命令是python modulename.py而不是modulename.py Yikes.哎呀。 Let this be a lesson for other python novices.让这成为其他 python 新手的教训。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入错误:没有名为 geopandas 的模块 - ImportError: No module named geopandas 如果在 anaconda 提示符下运行没有问题,但是 ModuleNotFoundError: No module named &#39;keras&#39; in Spyder - It's OK if run in anaconda prompt but ModuleNotFoundError: No module named 'keras' in Spyder Python文件:ImportError:在Eclipse中没有名为win32com.client的模块。 但是相同的文件在命令提示符下工作 - Python file :ImportError: No module named win32com.client in eclipse. But same file works in command prompt 使用 anaconda 提示安装 geopandas 会导致 ImportError - Installing geopandas with anaconda prompt causes ImportError 导入错误:没有使用 anaconda + windows 的名为“tensorflow”的模块 - ImportError: No module named 'tensorflow' using anaconda + windows 模块在 Anaconda 提示符下工作,但在 Spyder 中不工作 - A module is working in Anaconda prompt, but not in Spyder 从C#运行py脚本但从Windows命令行运行时,“没有名为fcntl的模块” - “No module named fcntl” when py script run from c# but works from windows command line 脚本在 Anaconda 提示但不能通过命令行工作 - Script works in Anaconda Prompt but not via Command Line ImportError:Spyder中没有名为tensorflow的模块 - ImportError: No module named tensorflow in Spyder ImportError:Spyder中没有名为“ tensorflow”的模块? - ImportError: No module named 'tensorflow' in Spyder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM