2016-10-21 4 views
1

Ich möchte ein Python 3-Programm in Python 3 öffnen, aber das Zielprogramm befindet sich in einem anderen Ordner.So öffnen Sie ein Python 3-Programm in einem anderen Ordner in Python 3

Beispiel: Pythonloader.py> PythonFolder> Target.py

Ich verwende Python 3.3.0

+1

Was bedeutet "offen"? Starten Sie, als ob Sie es manuell gestartet haben? 'das Modul importieren? Warum ist der separate Ordner speziell so ein Problem? – deceze

+0

Organisation ist wichtig. –

+1

Das beantwortet keine meiner Fragen. – deceze

Antwort

1

Verwenden der Subprozess Modul ein externes Programm auszuführen:

import subprocess 
subprocess.call('python3 /path/to/target.py') 

prüfen https://docs.python.org/2/library/subprocess.html für Eine Rezension.

Wenn Sie den Code importieren in Ihrem Skript zu verwenden:

import sys 
sys.path.append('/path/to/containing/directory') 
import target 

Dann können Sie den Code innerhalb verwenden.

+0

Das funktioniert sehr gut. Ich benutze dies für ein zentrales Python-Programm, um den Rest für die Schuleffizienz zu öffnen. –

0

Ihr Code funktioniert einwandfrei. Ich konnte die 4 Zahlen erraten und es hörte für mich richtig auf. Sieht aus, als hättest du das Programm richtig geschrieben, aber du kannst die Zahlen auch nicht erraten.

$ python foo.py 
First >> 1 
Second >> 2 
Third >> 3 
Fourth >> 4 
* 
First >> 5 
Second >> 6 
Third >> 7 
Fourth >> 8 

First >> 1 
Second >> 1 
Third >> 1 
Fourth >> 1 
** 
First >> 2 
Second >> 2 
Third >> 2 
Fourth >> 2 
* 
First >> 3 
Second >> 3 
Third >> 3 
Fourth >> 3 

First >> 4 
Second >> 4 
Third >> 4 
Fourth >> 4 
* 
First >> 1 
Second >> 1 
Third >> 2 
Fourth >> 4 
** 
First >> 1 
Second >> 2 
Third >> 1 
Fourth >> 4 
* 
First >> 1 
Second >> 1 
Third >> 4 
Fourth >> 2 
* 
First >> 4 
Second >> 1 
Third >> 1 
Fourth >> 2 

First >> 1 
Second >> 4 
Third >> 1 
Fourth >> 2 
** 
First >> 1 
Second >> 4 
Third >> 2 
Fourth >> 1 
**** 
+1

Ich habe jede mögliche Nummer ausprobiert und funktioniert immer noch nicht. –

+0

Ihr Code ist korrekt, aber Sie müssen intelligenter raten .. fragen Sie einen Freund zu versuchen – user2297550

Verwandte Themen