subprocess.popen和subprocess.run,在调用“file.py”时,都会在Linux上出现导入错误。 Linux上的Python 3.6.4。 Windows上的Python 3.6.5。 Windows工作没有问题。 Linux抛出错误。这些调用旨在独立于原始进程/脚本,并且不可见。
对于Linux(以下任一行):
import React from 'react';
import {Route, Link} from 'react-router-dom';
import FourthView from '../fourthview/fourthview.component';
import {Bootstrap, Grid, Row, Col, Button, Image, Modal, Popover} from 'react-bootstrap';
import traineeship from './traineeship.api';
import Header from '../header/header.component';
import InfiniteScroll from 'react-infinite-scroller';
require('./traineeship.style.scss');
class Traineeship extends React.Component {
constructor(props) {
super(props);
this.state = {
companies: [],
page: 0,
resetResult: false,
};
}
componentDidMount() {
this.fetchCompanies(this.state.page);
}
fetchCompanies(page){
traineeship.getAll(page).then(response => {
if (response.data) {
const companies = Array.from(this.state.companies);
this.setState({ companies: companies.concat(response.data._embedded.companies) });
// this.setState({companies: this.state.companies.concat(response.data._embedded.companies)});
} else {
console.log(response);
}
});
}
render() {
return (
<div className={"wrapperDiv"}>
{JSON.stringify(this.props.rootState)}
<div className={"flexDivCol"}>
<div id="header">
<Header/>
</div>
<div id="result">
<div className={"search"}>
<h2>Harjoittelupaikkoja</h2>
<p className={"secondaryColor"}>{this.state.companies.length} paikkaa löydetty</p>
</div>
<div className={"filters"}>
<h5 style={{marginTop: '30px', marginBottom: '10px'}} className={"primaryColor"}>
Hakukriteerit</h5>
<div className={"filter"}>Ravintola- ja cateringala</div>
<div className={"filter"}>Tarjoilija</div>
<div className={"filter"}>Kaikki</div>
</div>
<div className={"searchResults"}>
<h5 style={{marginTop: '30px', marginBottom: '10px'}} className={"primaryColor"}>
Hakutulokset</h5>
<InfiniteScroll
pageStart={0}
loadMore={() => this.fetchCompanies}
hasMore={true || false}
loader={<div className="loader" key={0}>Loading ...</div>}
useWindow={false}
>
{
this.state.companies.map((traineeship, key) => (
<div id={"item"} key={key}>
<div className={"companyInfo"}>
<div className={"heading"}>
<div id={"companyDiv"}>
<p style={{
fontSize: '18px',
lineHeight: '18px'
}}>{traineeship.name}</p>
</div>
{
traineeship.video == null
? ''
:
<div id={"videoDiv"}>
<div className={"youtubeBox center"}>
<div id={"youtubeIcon"}>
<a className={"primaryColor"}
href={traineeship.mediaUrl}>
<span style={{marginRight: '3px'}}><Image
src="http://www.stickpng.com/assets/images/580b57fcd9996e24bc43c545.png"
style={{
width: '24px',
height: '17px'
}}/></span>
<span> <p style={{
fontSize: '13px',
lineHeight: '18px',
margin: 0,
display: 'inline-block'
}}>Esittely</p></span>
</a>
</div>
<div id={"txtVideo"}>
</div>
</div>
</div>
}
</div>
<div className={"location"}>
<div id={"locationIcon"}>
<Image src="assets/img/icLocation.png" style={{marginTop: '-7px'}}/>
</div>
<div id={"address"}>
<a href={"https://www.google.com/maps/search/?api=1&query=" + encodeURI("Fredrikinkatu 4, Helsinki")}>
<p className={"primaryColor"}
style={{fontSize: '13px'}}>{traineeship.city}(show in
map)</p>
</a>
</div>
</div>
<div className={"companyDescription"}>
<p className={"secondaryColor"} style={{
fontSize: '14px',
lineHeight: '20px'
}}>{traineeship.description}</p>
</div>
<div className={"companyContacts"} style={{marginTop: '20px'}}>
<p className={"contactInfo"}>URL: {traineeship.website}</p>
<p className={"contactInfo"}>Email: {traineeship.email}</p>
<p className={"contactInfo"}>Puh: {traineeship.phonenumber}</p>
<p className={"contactInfo"}>Contact: {traineeship.contact}</p>
</div>
</div>
</div>
))
}
</InfiniteScroll>
</div>
</div>
</div>
</div>
);
}
}
export default Traineeship;
对于Windows(以下任一行):
proc = subprocess.run("python3 foo.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
proc = subprocess.Popen("python3 foo.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
另外,请注意,运行单个脚本可以顺利运行;只有当你从另一个脚本调用它时它才会出错。任何想法?
编辑: 例如,假设这是foo.py:
proc = subprocess.run("python bar.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
proc = subprocess.Popen("python bar.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
这是bar.py
import requests, urllib.request as urllib2, wget, ftplib, sys, tkinter as tk, tkinter.ttk as ttk, serial, threading, platform, time, socket, os, json, struct, logging, pickle, re, queue, subprocess, uuid, netifaces as nif, zipfile, ctypes
from platform import system as OS_VER
from PIL import Image, ImageTk
LibList = [requests, urllib2, wget, ftplib, sys, tk, ttk, serial, threading, platform, time, socket, os, json, struct, logging, pickle, re, queue, subprocess, uuid, nif, zipfile, ctypes, Image, ImageTk, platform]
for x in range(len(LibList)):
try: print(LibList[x].__file__)
except: print(LibList[x])
运行bar.py和foo.py在Linux上收到错误但在Windows上没有错误。 跳过bar.py并运行foo.py直接在Linux上工作正常。
答案 0 :(得分:0)
我找到的唯一解决方案是创建一个shell脚本并使用subprocess.Popen
或subprocess.run
进行调用。
例如:
subprocess.Popen('Foo.sh')
和Foo.sh文件:
#!/usr/bin/env bash
python3 Foo.py > log.log &
&amp;需要在Linux中分叉进程。否则,它会被阻止,直到关闭才会继续。