使用VENV和PYENV从命令行中运行Python脚本的问题
我创建了一个基本的Python脚本,该脚本称为API。在目录中,我还创建了一个虚拟环境:
python -m Venv Venv
我一直在运行脚本是vs Code没问题,并指向./ venv/bin/python
。
我想设置此功能,以便使用CRON作业重复。但是,我已经尝试运行以下命令...
/users/aaron/project/venv/bin/python/users/aaron/project/project/extract_starling.py.py
...并获得此错误:
File "/Users/aaron/project/extract_starling.py", line 9, in <module>
account_id = config['starling_config']['account_id']
File "/Users/aaron/.pyenv/versions/miniforge3/lib/python3.9/configparser.py", line 963, in __getitem__
raise KeyError(key)
KeyError: 'starling_config'
应该注意的是,当我运行哪个python
不指向虚拟环境时,我会得到...
/users/aaron/.pyenv/versions/miniforge3/bin/bin/bin/python
我设置pyenv
一会儿,尽管不知道为什么在说miniforge
。
无论如何,我想知道是否有人可以提供帮助,不太确定这里发生了什么。
编辑:
还应注意,我的脚本正在导入configparser
模块,如果相关,则在同一目录中使用config.conf
文件。这是我脚本的第一部分:
import requests
import configparser
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# Config data for Starling
config = configparser.ConfigParser()
config.read('config.conf')
account_id = config['starling_config']['account_id']
从pyenv global
3.9.1
输出从python -version
3.9.7 <
输出/code>
从pyenv local
输出
未配置的本地版本
从pyenv版本输出
system
3.10.0
3.6.15
3.8.10
* 3.9.1 (set by /Users/aaron/.pyenv/version)
miniforge3
miniforge3/envs/tf
I created a basic python script which calls an API among other things. Within the directory I also created a virtual environment:
python -m venv venv
I've been running the script is VS Code no problem, and have it pointing to ./venv/bin/python
.
I want to set this up so it repeats using a cron job. However, I've tried running the below command...
/Users/aaron/project/venv/bin/python /Users/aaron/project/extract_starling.py
...and get this error:
File "/Users/aaron/project/extract_starling.py", line 9, in <module>
account_id = config['starling_config']['account_id']
File "/Users/aaron/.pyenv/versions/miniforge3/lib/python3.9/configparser.py", line 963, in __getitem__
raise KeyError(key)
KeyError: 'starling_config'
Should note that when I run which python
when not pointing at a virtual environment, I get this...
/Users/aaron/.pyenv/versions/miniforge3/bin/python
I setup pyenv
a while, although don't know why it's saying miniforge
.
Anyway, I'm wondering if someone could assist, as not quite sure what's going on here.
EDIT:
Should also note that my script is importing the configparser
module, with a config.conf
file in the same directory, if that's relevant. This is the first part of my script:
import requests
import configparser
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# Config data for Starling
config = configparser.ConfigParser()
config.read('config.conf')
account_id = config['starling_config']['account_id']
Output from pyenv global
3.9.1
Output from python --version
3.9.7
Output from pyenv local
no local version configured
Output from pyenv versions
system
3.10.0
3.6.15
3.8.10
* 3.9.1 (set by /Users/aaron/.pyenv/version)
miniforge3
miniforge3/envs/tf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
认为这是我自己的错。
在我的脚本中,我替换了
config.read('config.conf
)`and and
and and and and and and and and foring。
基本上提供完整的路径而不是相对路径。
Think this was my own fault.
In my script, I replaced
config.read('config.conf
)`with
And it worked.
Basically providing the full path rather than the relative path.