简体   繁体   English

如何运行从bash导入yaml的python 3脚本

[英]How do I run a python 3 script that imports yaml from bash

I am calling a python script using python2.x from inside a bash script. 我正在从bash脚本内部使用python2.x调用python脚本。 How can I update it to python 3.X? 如何将其更新到python 3.X?

This is a script I need to be able to distribute to clients, using the venv cli will not work 这是我需要能够分发给客户端的脚本,使用venv cli无法正常工作

Running on CentOS 在CentOS上运行

First I've installed any number of python 3 packages. 首先,我已经安装了任意数量的python 3软件包。

$ sudo yum list installed | grep python3

python3-other-rpm-macros.noarch         3-25.el7                       @epel    
python34.x86_64                         3.4.10-2.el7                   @epel    
python34-devel.x86_64                   3.4.10-2.el7                   @epel    
python34-libs.x86_64                    3.4.10-2.el7                   @epel    
python34-pip.noarch                     8.1.2-8.el7                    @epel    
python34-setuptools.noarch              39.2.0-3.el7                   @epel    
python36.x86_64                         3.6.8-1.el7                    @epel    
python36-libs.x86_64                    3.6.8-1.el7                    @epel    
$ sudo pip3.4 install pyyaml
Requirement already satisfied (use --upgrade to upgrade): pyyaml in /usr/lib64/python3.4/site-packages

from bash script do_update.sh 从bash脚本do_update.sh

#!/bin/bash

python3 update_yaml.py

from python script update_yaml.py 来自python脚本update_yaml.py

import sys
import common_update
import subprocess

with open.('input.yaml') as in_yaml:
  input_data = yaml.safe_load(in_yaml)

I expect it to parse input.yaml 我希望它能够解析input.yaml

Output: "ModuleNotFoundError: No module named 'yaml'" 输出: "ModuleNotFoundError: No module named 'yaml'"

It looks like you might have multiple versions of python installed. 看来您可能安装了多个版本的python。

It's no longer recommended to use the pip script (or pip3.4 etc.) A common problem is that you actually install packages for a different python version than the one you expected. 不再建议使用pip脚本(或pip3.4等)。一个常见的问题是,您实际上为与预期版本不同的python版本安装了软件包。

Instead do this: 而是这样做:

sudo python3 -m pip install pyyaml

This way you can be sure that python3 will be able to use the library. 这样,您可以确定python3将能够使用该库。

Do not ever run pip install under root in CentOS/RHEL unless it happens inside virtualenv , or unless you want to screw your system. 永远不要运行pip install下的CentOS / RHEL根除非它里面virtualenv ,或除非你想搞砸你的系统。

There is package: 包:

yum install python34-PyYAML

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

相关问题 如何在不中断上层导入的情况下从子目录运行 Python 脚本? - How do I run a Python script from a subdirectory without breaking upper-level imports? 如何从调用 python 脚本的 bash 脚本到调用 bash 脚本的 python 脚本? - How do I go from a bash script that calls a python script to a python script calling a bash script? 如何在Python中运行bash脚本,但就好像它从另一个目录运行? - How do I run a bash script inside Python, but act as if it's running from another directory? 如何使用bash脚本从其他目录运行代码 - How do I run code from a different directory with a bash script 如何与另一个脚本共享python脚本的导入? - How do I share a python script's imports with another script? 当我的主要脚本位于整个包的子文件夹中时,如何从python脚本导入? - How do I do imports from a python script when my main scripts are in a subfolder of the whole package? 如何从 bash 脚本运行 python 文件 - How to run a python file from a bash script 如何从 python 脚本中获取导入和依赖文件的列表? - How do I get the list of imports and dependant files from python script? 如何从 Photoshop 脚本运行 Python 脚本? - How do I run a Python script from a Photoshop script? 如何在由谷歌云存储完成事件触发的 python 脚本中运行 bash 脚本? - How do I run a bash script inside python script triggered by google cloud storage finalize event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM