2015-06-03 9 views
5

Attualmente ho Python 3.4 come la mia versione predefinita di Python, ma voglio impostare temporaneamente Python 2.7 come predefinito.L'avvio di Python per Windows non sta leggendo `py.ini`

Sono su Windows 7 e i miei script Python vengono eseguiti utilizzando l'avvio di Windows Python. The documentation says Posso personalizzarlo creando un file py.ini, ma non funziona. Ho creato un file con questi contenuti:

[defaults] 
python=2.7 

ho cercato di inserirlo nella stessa cartella del file sto correndo, ho provato a metterlo in C:\Users\Administrator\, in C:\Users\Administrator\AppData\ e in C:\Users\Administrator\AppData\Local\, ma nessuno di questi lavorato. Il programma di avvio utilizza ancora Python 3.4. (Sia quando si fa doppio clic sul file in Windows UI e sia quando lancio il programma di avvio direttamente, come py my_file.py.)

Perché il programma di avvio di Windows Python ignorando il mio file py.ini?

Ecco l'output esecuzione py age.py con l'ambiente variabile PYLAUNCH_DEBUG serie:

launcher build: 32bit                 
launcher executable: Console               
Using local configuration file 'C:\Users\Administrator\AppData\Local\py.ini'   
File 'C:\Windows\py.ini' non-existent             
Called with command line: age.py              
maybe_handle_shebang: read 256 bytes             
maybe_handle_shebang: BOM not found, using UTF-8          
parse_shebang: found command: python             
searching PATH for python executable             
Python on path: C:\python34\python.EXE             
located python on PATH: C:\python34\python.EXE           
run_child: about to run 'C:\python34\python.EXE age.py'        
Traceback (most recent call last):              
    File "age.py", line 17, in <module>             
    name = raw_input("Enter a person's name to check their age: ")      
NameError: name 'raw_input' is not defined            
child process exit code: 1 
+0

provare a utilizzare uno shebang, '#!/bin/env python2.7' nella parte superiore dello script facendo riferimento alla posizione del' Python27'!? – ZdaR

+0

Imposta la variabile di ambiente 'PYLAUNCH_DEBUG' su qualsiasi valore ed esegui lo script usando' py my_file.py' in una finestra del prompt dei comandi. Che risultati ti dà questo? –

+0

@ ZdaR È importante che questo script specifico non abbia uno shebang. –

risposta

1

Il documentation for Python 3.5 descrive questo comportamento:

La /usr/bin/env forma di shebang ha una ulteriore proprietà speciale. Prima di cercare gli interpreti Python installati, questo modulo cercherà l'eseguibile PATH per un eseguibile Python. Ciò corrisponde al comportamento del programma Unix env, che esegue una ricerca PATH.

Stranamente questa stessa funzionalità sembra applicarsi a Python 3.4 come bene (o almeno la versione 3.4.3), nonostante la corresponding documentation page for Python 3.4 non citarlo. Ho incluso una riproduzione di questo comportamento in fondo a questa risposta.

Sembra lo script contiene la linea shebang #!/usr/bin/env python in alto, e C:\Python34 è sul sistema di PATH prima di qualsiasi apparenza di C:\Python27. Lei dice in un commento che

E 'importante per questo script specifica di non avere uno shebang

ma la linea

parse_shebang: found command: python 

nell'output lanciatore commette il fatto che lo script deve infatti avere una linea di shebang.


devo Python 2.7.10 e Python 3.4.3 installato sul mio sistema, con 3.4 prima di 2.7 sulla PATH.Ho anche un file py.ini in C:\Users\Luke\AppData\Local che contiene quanto segue:

[defaults] 
python=2 

e uno script che contiene test.py

#!/usr/bin/env python 
import sys; print(sys.version_info) 

Ho impostato il valore della variabile d'ambiente PYLAUNCH_DEBUG a 1. L'esecuzione di questo script utilizzando py test.py, ottengo il seguente output:

launcher build: 32bit 
launcher executable: Console 
Using local configuration file 'C:\Users\Luke\AppData\Local\py.ini' 
File 'C:\WINDOWS\py.ini' non-existent 
Called with command line: test.py 
maybe_handle_shebang: read 60 bytes 
maybe_handle_shebang: BOM not found, using UTF-8 
parse_shebang: found command: python 
searching PATH for python executable 
Python on path: C:\Python34\python.EXE 
located python on PATH: C:\Python34\python.EXE 
run_child: about to run 'C:\Python34\python.EXE test.py' 
sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0) 
child process exit code: 0 

Se cambio il mio script test.py a

#! python 
import sys; print(sys.version_info) 

(cioè rimuovere il /usr/bin/env dalla linea shebang) e rieseguire py test.py, ho il seguente:

launcher build: 32bit 
launcher executable: Console 
Using local configuration file 'C:\Users\Luke\AppData\Local\py.ini' 
File 'C:\WINDOWS\py.ini' non-existent 
Called with command line: test.py 
maybe_handle_shebang: read 48 bytes 
maybe_handle_shebang: BOM not found, using UTF-8 
parse_shebang: found command: python 
locating Pythons in 64bit registry 
locate_pythons_for_key: unable to open PythonCore key in HKCU 
locate_pythons_for_key: unable to open PythonCore key in HKLM 
locating Pythons in native registry 
locate_pythons_for_key: unable to open PythonCore key in HKCU 
locate_pythons_for_key: C:\Python27\python.exe is a 32bit executable 
locate_pythons_for_key: C:\Python27\PCBuild\python.exe: The system cannot find the path specified. 
locate_pythons_for_key: C:\Python27\PCBuild\amd64\python.exe: The system cannot find the path specified. 
locate_pythons_for_key: C:\Python34\python.exe is a 32bit executable 
locate_pythons_for_key: C:\Python34\PCBuild\python.exe: The system cannot find the path specified. 
locate_pythons_for_key: C:\Python34\PCBuild\amd64\python.exe: The system cannot find the path specified. 
found configured value 'python=2' in C:\Users\Luke\AppData\Local\py.ini 
search for default Python found version 2.7 at 'C:\Python27\python.exe' 
run_child: about to run 'C:\Python27\python.exe test.py' 
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0) 
child process exit code: 0 
+0

Ragionamento solido, ma non funziona anche dopo aver rimosso la linea dello shebang. Esegui l'output di debug qui: https://gist.github.com/cool-RR/b2f5e086a7adaeaf5cbd –

+0

@RamRachum, ma ora il tuo output di debug dice che non riesce a trovare py.ini: "File" C: \ Users \ Administrator \ AppData \ Local \ py.ini 'inesistente' ". Ma "" trovato valore configurato "python = 3" in ambiente' ", cioè la variabile di ambiente' PY_PYTHON = 3' è impostata. – eryksun

+0

@eryksun Spiacente, probabilmente il mio errore, ma ora mi sono assicurato che il file esista e ancora non funzioni: https://gist.github.com/cool-RR/adbd67d87944a5971f0e –

Problemi correlati