使用VENV和PYENV从命令行中运行Python脚本的问题

发布于 2025-02-04 10:55:43 字数 1692 浏览 3 评论 0原文

我创建了一个基本的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2025-02-11 10:55:43

认为这是我自己的错。

在我的脚本中,我替换了

config.read('config.conf)`

and and

import os
cwd = os.path.dirname(os.path.abspath(__file__))
config.read(f'{cwd}/config.conf')

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

import os
cwd = os.path.dirname(os.path.abspath(__file__))
config.read(f'{cwd}/config.conf')

And it worked.
Basically providing the full path rather than the relative path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文