Seite 1 von 1

"subprocess" importiert keine Librarys

Verfasst: Sonntag 19. April 2020, 17:19
von SchusselHD
Guten Tag,

wenn ich "subprocess" nutze und damit eine andere Datei mit anderen Librarys öffnen möchte passiert folgendes: no module named "numpy". Dies dürfte eigentlich nicht passieren, denn alle Dateien liegen im gleichen Order, sind im gleichen Projekt und "numpy" ist erfolgreich installiert. Ich arbeite mit Pycharm.

Code: Alles auswählen

subprocess.call(['python.exe','x.py'])
ist der auszuführende Teil.

Re: "subprocess" importiert keine Librarys

Verfasst: Sonntag 19. April 2020, 17:58
von __blackjack__
@SchusselHD: Das ist dann ein anderes Python. Wenn Du das Python verwenden willst das diesen Aufruf macht, nimm `sys.executable` statt "python.exe".

In neuem Code besser `subrocess.run()` statt `call()`.

Re: "subprocess" importiert keine Librarys

Verfasst: Montag 20. April 2020, 09:35
von DeaD_EyE
Normalerweise importiert man Module.
Dein Problem wird sein, dass du wahrscheinlich Python einmal Systemweit und in %localappdata%\Programs\ installiert haben wirst.
Du rufst den Interpreter direkt mit python.exe auf. Welche Version dann gestartet wird, hängt davon ab, welcher Pfad in die Umgebungsvariable eingetragen ist.
Das was zuerst kommt, findet er auch zuerst.

Deswegen haben die Python-Entwickler `py.exe` für Windows seit Python 3.5 herausgebracht.
Das Programm sucht an den bekannten Orten und ermöglicht dem Nutzer eine einfach Wahl der Version.

Code: Alles auswählen

Launcher arguments:

-2     : Launch the latest Python 2.x version
-3     : Launch the latest Python 3.x version
-X.Y   : Launch the specified Python version
     The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32  : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64  : Launch the latest 64bit Python X version
-0  --list       : List the available pythons
-0p --list-paths : List with paths


Im Code könnte das so aussehen:

Code: Alles auswählen

import pprint
import subprocess
import sys


def get_installed_versions():
    versions = []
    output = subprocess.check_output(["py", "-0"], encoding="utf8").splitlines()
    for line in output:
        line = line.strip()
        if not line:
            continue
        version, arch = line.lstrip("-").split("-")
        versions.append((version, arch))
    return versions


def run(*args):
    command = [sys.executable, *args]
    return subprocess.run(command)


def run_py_exe(version, arch, *args):
    if arch not in (32, 64, "32", "64"):
        raise ValueError(f"{arch} invalid. Choose 32 or 64")
    command = ["py", f"-{version}-{arch}", *args]
    print(command)
    return subprocess.run(command)



versions = get_installed_versions()

print("Current Environment")
print(sys.version)
print(sys.executable)
pprint.pprint(sys.path)
print("Running  external Python Script with same interpreter")
print("========")
print()
run("x.py")
print()

print("Using py.exe to find the spcified version and arch of Python interpreter")
for version, arch in versions:
    run_py_exe(version, arch, "x.py")
    print()

Und die x.py:

Code: Alles auswählen

from __future__ import print_function
import sys


print("External Python Script")
print(sys.version)
print(sys.executable)
Dann rufe ich das erste Programm `y.py`auf, dass die anderen Interpreter starten soll:

Code: Alles auswählen

py -3 .\y.py
`py -3` selektiert die höchste installierte verfügbare 64 bit version von Python. Wenn 64 bit nicht verfügbar ist, wird die 32 bit Variante selektiert.
Die Ausgabe sieht bei mir entsprechend so aus:

Code: Alles auswählen

PS C:\Users\Admin\Desktop\testtest> py -3 .\y.py
Installed Pythons found by py Launcher for Windows *

Current Environment
3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)]
C:\Users\Admin\AppData\Local\Programs\Python\Python38\python.exe
['C:\\Users\\Admin\\Desktop\\testtest',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\lib',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\win32',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\Pythonwin']
Running  external Python Script with same interpreter
========

External Python Script
3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)]
C:\Users\Admin\AppData\Local\Programs\Python\Python38\python.exe

Using py.exe to find the spcified version and arch of Python interpreter
['py', '-3.8-64', 'x.py']
External Python Script
3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)]
C:\Users\Admin\AppData\Local\Programs\Python\Python38\python.exe

['py', '-3.8-32', 'x.py']
External Python Script
3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)]
C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\python.exe

['py', '-3.7-64', 'x.py']
External Python Script
3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)]
C:\Users\Admin\AppData\Local\Programs\Python\Python37\python.exe

['py', '-3.7-32', 'x.py']
External Python Script
3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 09:44:33) [MSC v.1900 32 bit (Intel)]
C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\python.exe

['py', '-2.7-32', 'x.py']
External Python Script
2.7.1 Stackless 3.1b3 060516 (release27-maint, Jan  1 2011, 13:04:37) [MSC v.1500 32 bit (Intel)]
C:\Python27\python.exe
Wie man deutlich erkennen kann, hab ich gemischte Installationen.
Einmal Stackless Python2.7 Systemweit und die anderen als lokale Anwendungen.

Dennoch, ein import ist immer vorzuziehen.

PS: Hab gerade gesehen, dass ich Python updaten muss ^^