简体   繁体   中英

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:

  1. Create new directory structure
  2. Download a.zip file from a URL and unzip contents
  3. Clean up the data
  4. Export data as 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"

I am using Windows 10 Enterprise version 1909, conda v4.9.2, Anaconda command line client 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:

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:

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. I thought I had added the necessary Anaconda file paths to my Windows PATH variable before.

The path to my virtual environment packages are in "C:\Users\KWOODW01\Anaconda3\envs\pygis\Lib\site-packages"

echo %PATH% returns:

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". 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. The proper command is python modulename.py instead of modulename.py if you want to execute a.py file from the command prompt. Yikes. Let this be a lesson for other python novices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM