简体   繁体   English

从命令行运行 python 脚本有很多麻烦

[英]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.你如何回答的上下文:我是命令行环境的新手,除了基本的 git 命令。 I've always just used an IDE like PyCharm or NetBeans to run stuff for school projects.我一直只使用像 PyCharm 或 NetBeans 这样的 IDE 来运行学校项目的东西。 Please frame your answer accordingly.请相应地框定您的答案。

I have a very small python script that pulls down a URL:我有一个非常小的 python 脚本,可以拉下一个 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.我做了sys.path.append()是因为我读到了一个不同的问题,它可以解决从命令行运行我的脚本时“找不到模块”的问题。

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 .不要使用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.相反,您应该使用pip来安装您的软件包,因为我假设您一直在使用 PyCharm 软件包管理器。 I think it automatically installs with current versions of python (not sure though, I'm on Linux) and it's used like so:我认为它会自动安装当前版本的 python(虽然不确定,我在 Linux 上)并且它是这样使用的:

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.我以前从未使用过py命令(我错过了吗?),请尝试使用python bloomtest.py来运行它,只是为了确定。 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.您可能会收到一条错误消息,告诉您python是一个无法识别的命令或文件,如果是这种情况,则意味着您的 PATH 设置不正确。 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.我发现解决此问题的最简单方法是简单地重新安装 python,确保选中将 python 添加到您的 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.我想发布我的问题的最终解决方案,由于DutChen18的回答,我偶然发现了这个问题。

He said I should use pip install to install all of my packages, which is one thing that I already do.他说我应该使用pip install来安装我所有的包,这是我已经在做的一件事。 I don't know much about the command line, except basic git and that.我对命令行知之甚少,除了基本的 git 之类的。 Trying to do it again gave me requirement already satisfied errors.再次尝试这样做给了我requirement already satisfied错误。

However, I was using an embedded terminal in PyCharm: C:\\Program Files\\Git\\bin\\bash.exe , which comes when you download Git.但是,我在 PyCharm 中使用了一个嵌入式终端: C:\\Program Files\\Git\\bin\\bash.exe ,它在您下载 Git 时出现。 This works great in PyCharm because it automatically starts in the working directory of your project.这在 PyCharm 中非常有效,因为它会自动在项目的工作目录中启动。 Very convenient for me, a new command line user.对我这个新的命令行用户来说非常方便。

I decided to open up Git Bash separately from PyCharm and run pip install again.我决定与 PyCharm 分开打开 Git Bash 并再次运行pip install The first thing I found is that it didn't work without python -m pip install , unlike in the PyCharm embedded terminal.我发现的第一件事是,它在没有python -m pip install情况下无法工作,这与 PyCharm 嵌入式终端不同。

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.一旦我想通了,我尝试python -m pip install BloomSky-API ,但这一次它没有告诉我这些库已经安装。 All of a sudden, I could run my python script from the command line.突然之间,我可以从命令行运行我的 python 脚本。

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.我对为什么会发生这种情况或为什么现在有效,我有零线索,而且我现在很想听到更技术性的解释,因为我已经有了工作。

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

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