Avatar billede friiiiis Novice
02. marts 2023 - 13:25 Der er 2 løsninger

Python --version

Hi,

I'm using Spyder (Python version 3.8)

When I'm trying to se the version of python I'm writing

python --version

in the console window; Python responds with:
"NameError: name 'python' is not defined"

BUT - If I write:

!{sys.executable}  --version

in the console window; Python responds with:
"Python 3.8.3"

Why this difference? Why can't I call "Python" but have to use "!{sys.executable}" ?

I have the same problem if I want to call a script - "python" does not work; I have to use "!{sys.executable}"
Avatar billede arne_v Ekspert
02. marts 2023 - 15:39 #1
Så vidt jeg kan google mig til så bruger du ikke Python men Python+jupyterlab/IPython.

Og i jupyterlab/IPython bruges ! til at udføre shell kommandoer i.s.f. Python kommandoer.

python er en Python kommando python
!python er en shell kommando python
!{{sys.executable} er en shell kommando med hvad sys.executable nu indeholder
Avatar billede Keld Broe Seniormester
06. marts 2023 - 15:07 #2
The reason why python --version is not working in the Spyder console window is because python is not recognized as a command or executable on the system's PATH. The !{sys.executable} --version command is working because it directly calls the Python executable that is associated with the currently running Python environment.

sys.executable is a Python variable that returns the absolute path of the current Python interpreter that is being used to run the script. Using !{sys.executable} ensures that the correct Python interpreter is being used to execute the command, even if there are multiple versions of Python installed on the system.

If you want to call a Python script, you can either use the !{sys.executable} script_name.py command or specify the absolute path of the Python interpreter followed by the path to the script, like C:\Python38\python.exe script_name.py.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester