简体   繁体   中英

Lots of trouble running a python script from the command line

Context for how you answer: I am new to the command line environment, except when it comes to basic git commands. I've always just used an IDE like PyCharm or NetBeans to run stuff for school projects. Please frame your answer accordingly.

I have a very small python script that pulls down a URL:

import sys
sys.path.append(r'C:\Users\WNeill\PycharmProjects\bloomskyGrantGrove\venv\Lib\site-packages\bloomsky_api')
import bloomsky_api as bs

client = bs.BloomSkyAPIClient(api_key='pr-XXXXXXXXXX')
data = client.get_data()[0] # Dictionary formatted like JSON, if you want data besides the latest image

with open("image_URL.txt", 'w') as file:
    print(data.get('outdoor').get('image_url'), file=file)

I did the sys.path.append() because I read in a different question that it would solve my problems of "module not found" when running my scripts from the command line.

Well, it did, sort of... now, it finds my imports, but apparently my imports have imports...

$ py -m bloomtest.py Traceback (most recent call last): File "C:\\Program Files\\Python38\\lib\\runpy.py", line 183, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "C:\\Program Files\\Python38\\lib\\runpy.py", line 109, in _get_module_details __import__(pkg_name) File "C:\\Users\\WNeill\\PycharmProjects\\bloomskyGrantGrove\\bloomtest.py", line 4, in <module> import bloomsky_api as bs File "C:\\Users\\WNeill\\PycharmProjects\\bloomskyGrantGrove\\venv\\Lib\\site-packages\\bloomsky_api\\bloomsky_api.py", line 2, in <module> import requests ModuleNotFoundError: No module named 'requests'

So what do I do to make this work when my dependencies have dependencies?

Don't use sys.path.append . Imagine sending your code to someone else, their packages won't be in the same path and they won't be able to run your program. You might not plan on distributing your code but it's just bad practice.

Instead you should use pip to install your packages, as i assume you've been using the PyCharm package manager. I think it automatically installs with current versions of python (not sure though, I'm on Linux) and it's used like so:

pip install BloomSky-API

it'll automatically get all the dependencies and put them in the right places.

I've never used the py command before (am I missing out?), try using python bloomtest.py to run it instead just to be sure. You might get an error telling you that python is an unrecognized command or file, if that's the case it means your PATH is not set up correctly. I've found the easiest way to resolve this is to simply reinstall python, making sure to check the checkbox that says to add python to your PATH.

I'd usually post suggestions like this in a comment if I'm not sure if it solves the problem you're having, but the answer is too long to fit in a comment. Hope this helps!

I want to post the final solution to my problem, which I stumbled on thanks to DutChen18 's answer.

He said I should use pip install to install all of my packages, which is one thing that I already do. I don't know much about the command line, except basic git and that. Trying to do it again gave me requirement already satisfied errors.

However, I was using an embedded terminal in PyCharm: C:\\Program Files\\Git\\bin\\bash.exe , which comes when you download Git. This works great in PyCharm because it automatically starts in the working directory of your project. Very convenient for me, a new command line user.

I decided to open up Git Bash separately from PyCharm and run pip install again. The first thing I found is that it didn't work without python -m pip install , unlike in the PyCharm embedded terminal.

Once I figured that out, I tried to python -m pip install BloomSky-API , but this time it didn't tell me that the libraries were already installed. All of a sudden, I could run my python script from the command line.

I have ZERO clue as to why this happened or why it now works, and I would love to hear a more technical explanation now that I have things working.

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