简体   繁体   English

Pyenv Python 版本在 Linux Shell 脚本中不起作用

[英]Pyenv Python versions not working in Linux Shell scripts

I'm trying to create shell script on Linux (Ubuntu) as presented in Automate the boring stuff with Python我正在尝试在 Linux (Ubuntu) 上创建 shell 脚本,如使用 Python 自动化这些无聊的东西所述

I've created all the files and Python script and when I'm trying to run it from Dash in Ubuntu it runs Python script with Python version that is not in my pyenv global version.我已经创建了所有文件和 Python 脚本,当我尝试在 Ubuntu 中从 Dash 运行它时,它运行的 Python 脚本的 Python 版本不在我的 pyenv 全局版本中。 Because of that i can't use any additional modules installed via pip.因此,我无法使用通过 pip 安装的任何其他模块。

My Python versions (as listed with pyenv versions)我的 Python 版本(与 pyenv 版本一起列出)

  system
* 3.8.1 (set by /home/tomek/.python-version)
  3.8.1/envs/blog_env
  3.8.1/envs/ll_env
  3.8.1/envs/mcam_env
  3.8.1/envs/money_value
  3.8.1/envs/pizzaenv
  3.8.1/envs/project_2
  blog_env
  ll_env
  mcam_env
  money_value
  pizzaenv
  project_2

version 3.8.1 is set as global with pyenv.版本 3.8.1 使用 pyenv 设置为全局。 Some details:一些细节:

which python && which pip && python -V
/home/tomek/.pyenv/shims/python
/home/tomek/.pyenv/shims/pip
Python 3.8.1

My .sh (placed in my home directory) file looks like this我的 .sh(放在我的主目录中)文件看起来像这样

#!/usr/bin/env bash
python3 /home/tomek/automate/MCAM/mClip.py 'agree'
bash

My .desktop file (placed in .local/share/applications needed to run script via Dash in Ubuntu) looks like this我的 .desktop 文件(放置在 .local/share/applications 需要通过 Dash 在 Ubuntu 中运行脚本)看起来像这样

[Desktop Entry]
Name=mClip
Exec=gnome-terminal -- /home/tomek/mClip.sh
Type=Application
Categories=GTK;GNOME;Utility;

and my first line in Python script looks like this:我在 Python 脚本中的第一行如下所示:

#!/usr/bin/env python

So, when i run my script from shell it works fine, but when i run it via Dash it gets error because of missing modules, so it's trying to run it with different Python environment.所以,当我从 shell 运行我的脚本时,它工作正常,但是当我通过 Dash 运行它时,由于缺少模块而出错,因此它试图在不同的 Python 环境中运行它。 I tried to debug this script the same way i tried to run it so i changed .sh file this way我尝试以与尝试运行它相同的方式调试此脚本,因此我以这种方式更改了 .sh 文件

#!/usr/bin/env bash
which python && which pip && python -V && python3 -V
bash

and i get this result我得到了这个结果

/usr/bin/python
/usr/bin/pip
Python 2.7.17
Python 3.6.9

When i then run this code in same shell window (!) it gives me当我然后在同一个 shell 窗口(!)中运行此代码时,它给了我

/home/tomek/.pyenv/shims/python
/home/tomek/.pyenv/shims/pip
Python 3.8.1
Python 3.8.1

I've tried also to run pyenv in this .sh script to see whats the outcome but it gives me error that pyenv command is not found.我也试过在这个 .sh 脚本中运行 pyenv 以查看结果是什么,但它给了我找不到 pyenv 命令的错误。 I've tried to look everywhere for some answers but not a single one worked for me.我试图到处寻找一些答案,但没有一个对我有用。

I figured out answer for my own question.我想出了我自己问题的答案。

Installing and configuring pyenv as user will not make it work for sh files ran from Ubuntu Dash.以用户身份安装和配置 pyenv 不会使其适用于从 Ubuntu Dash 运行的 sh 文件。 When you configure pyenv you configure bash configuration files (like .bashrc).当您配置 pyenv 时,您将配置 bash 配置文件(如 .bashrc)。

This configuration files are only loaded when you start bash, not when you run some other program such as sh (not even if bash is invoked via the name sh).此配置文件仅在您启动 bash 时加载,而不是在您运行其他程序(例如 sh)时加载(即使通过名称 sh 调用 bash 也不加载)。 And it's only loaded when bash is interactive.它仅在 bash 交互式时加载。

You have to run virtual environment directly like this (this is for default pyenv install location).您必须像这样直接运行虚拟环境(这是默认的 pyenv 安装位置)。

For example, checking python version:例如,检查python版本:

/home/<username>/.pyenv/versions/<environment_name>/bin/python -V

Going back to my case.回到我的案例。 My sh file looks like this now:我的 sh 文件现在看起来像这样:

#!/usr/bin/env bash
python -V
/home/tomek/.pyenv/versions/mcam_env/bin/python -V
bash

and the result is结果是

Python 2.7.17 #This is default Python version for root
Python 3.8.1 #This is my pyenv Python version

Futher reading:延伸阅读:

In your Python script your first line says #!/usr/bin/env python , so I think this redirects to /usr/bin/python , which is the wrong one.在你的 Python 脚本中,你的第一行说#!/usr/bin/env python ,所以我认为这重定向到/usr/bin/python ,这是错误的。 Could you try #!/usr/bin/env python3 ?你能试试#!/usr/bin/env python3吗?

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

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