Python Forum
Script File Failure-Path Error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script File Failure-Path Error?
#1
I am new to Python and this forum (but not general programming) and looking for help in understanding how Python Interpreter interacts with PATH and PYTHONPATH environment variables in Windows 11 64bit environment.

I have successfuly installed python 3.11 in Windows 11 and created a a simple "hello_world.py" script file in a sub-folder under the main python installtion folder.

The script file runs correctly within IDLE and the CMD prompt as hello_world.py but am getting execution failures under other execution modes caused by durectory/file errors. See attached file for details.

The problem is my lack of a clear understanding of how python interacts with the Windows envionment (especially path variables) and searching other online sources and documentation has not resolved the problem.

Any guidance or pointers to definitive resources rekated to python and the Windows environment gratefully received.

Apologies for any forum rules I have inadvertently breached

Thank you - Jerry


.docx   Script File Failure & Python Path Error.docx (Size: 13.45 KB / Downloads: 118)
Larz60+ write Nov-27-2022, 04:11 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Your enviroment path variables look ok.
Open cmd and do this.
C:\>python -V
Python 3.10.5

C:\>pip -V
pip 22.3 from C:\python310\lib\site-packages\pip (python 3.10)
C:\>
For your this should work and point to Python 3.11.

The problem is that you most run it with whole file name include .py file extensions.
So like this.
C:\Users\jerry>python hello_world.py
Reply
#3
This post updates my first attempt and should conform more to the forum rules so apologies to anyone who has had to try and decipher my first attempt and also apologies for repeating some of the initial text.

I am new to Python (but not general programming) and looking for help in understanding how Python Interpreter interacts with PATH and PYTHONPATH environment variables in Windows 11 64bit environment.

Python 3.10 initially installed from Microsoft Store but then uninstalled for later 3.11 version from python.org.

Python 3.11 installed in C:\python311 using Python.org download and selecting "Add Python to PATH" during installation.
Windows Path includes relevant standard python entries and both IDLE and python launch correctly.

Sub directory C:\python311\my_python_files created to store user script files.
C:\python311\my_python _files sub-directory added to User environment variables and additional PYTHONPATH C:\python311\my_python_files
also added to User environment variables.

The current User path variables are:
Output:
C:\python311\my_python_files> echo %path% C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS; C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\; C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL; C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Windows Live\Shared; C:\python311\my_python_files;C:\python311\my_python_files;C:\python311\Scripts\;C:\python311\; C:\Users\jerry\AppData\Local\Microsoft\WindowsApps;
I note C:\python\my_python_files appears twice, presumably from Path and PYTHONPATH assignments). This last entry (C:\Users\jerry\AppData\Local\Microsoft\WindowsApps;) may be a leftover from initial Microsft Store install.

Sys.path variables when imported are:
>>> import sys
>>> sys.path
Output:
['', 'C:\\python311\\my_python_files', 'C:\\python311\\python311.zip', 'C:\\python311\\Lib', 'C:\\python311\\DLLs', 'C:\\python311', 'C:\\python311\\Lib\\site-packages']
Simple hello_world.py script file saved in C:\Python311\my_python_files. Script runs correctly from IDLE and also from Cmd line C:\Users\jerry>hello_world.py

The following attempts to run the script all failed:

C:\Users\jerry>python hello_world
Error:
python: can't open file 'C:\\Users\\jerry\\hello_world': [Errno 2] No such file or directory
C:\python311\my_python_files>python hello_world
Error:
python: can't open file 'C:\\python311\\my_python_files\\hello_world': [Errno 2] No such file or directory
C:\Users\jerry>python
Output:
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> hello_world
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'hello_world' is not defined >>>
[/python]C:\python311\my_python_files>python[/python]
Output:
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> hello_world
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'hello_world' is not defined >>>
The script runs correctly when "Imported" into the Python Interpreter and also using runpy.

The problem is undoubtedly my lack of a clear understanding of how python interacts with the Windows environment (especially path variables) and searching other online sources and documentation has caused more confusion than it has resolved.

Any guidance or pointers to definitive resources related to python and the Windows environment gratefully received.
Reply
#4
Read my post again you are not doing what i post.
Again you most use whole file name when you run it from command line C:\Users\jerry>python hello_world.py
Reply
#5
(Nov-27-2022, 08:26 PM)snippsat Wrote: Read my post again you are not doing what i post.
Again you most use whole file name when you run it from command line C:\Users\jerry>python hello_world.py

Hello snipsat and thanks for your patience and 2 replies. My second post was just a revamp of my first one but using the forums BBCode conventions.

You are quite right that if I use the correct .py extension from the directory containing the script then it executes correctly (see below), so many thanks for that correction.

C:\python311\my_python_files>python hello_world.py
Output:
Hello World
However, I am still puzzled by how the interpreter interacts with Windows environment since any attempt to execute the script using the cmd line from any other directory results in failure, even though the Path and PYTHONPATH all include the sub-directory containing the script.
Reply
#6
(Nov-27-2022, 08:54 PM)jerryf Wrote: However, I am still puzzled by how the interpreter interacts with Windows environment since any attempt to execute the script using the cmd line from any other directory results in failure
Run what i did in cmd in first post and post result.
C:\>python -V
Python 3.10.5
 
C:\>pip -V
pip 22.3 from C:\python310\lib\site-packages\pip (python 3.10)
C:\>
Your sys.path look correct as it finds C:\\python311.
So this shoud be the folder where you have installed Python?
I have tutorial here you can look at,the installer is the same for Python 3.11.
Reply
#7
(Nov-27-2022, 09:14 PM)snippsat Wrote:
(Nov-27-2022, 08:54 PM)jerryf Wrote: However, I am still puzzled by how the interpreter interacts with Windows environment since any attempt to execute the script using the cmd line from any other directory results in failure
Run what i did in cmd in first post and post result.
C:\>python -V
Python 3.10.5
 
C:\>pip -V
pip 22.3 from C:\python310\lib\site-packages\pip (python 3.10)
C:\>
Your sys.path look correct as it finds C:\\python311.
So this shoud be the folder where you have installed Python?
I have tutorial here you can look at,the installer is the same for Python 3.11.

Here are the results of running the 2 cmds in your first reply :

C:\Users\jerry>python -V
Output:
Python 3.11.0
C:\Users\jerry>pip -V
Output:
pip 22.3 from C:\python311\Lib\site-packages\pip (python 3.11)
Reply
#8
(Nov-27-2022, 11:00 PM)jerryf Wrote: Here are the results of running the 2 cmds in your first reply :
Then Python Windows Path is ok✨
This mean that you can run python command from any folder,
of course the file most be in folder you are in,or have give to give path to file.
Example hello.py is not in Users folder then it don't work,in this case have to give path to file the it work.
C:\Users>python hello.py
python: can't open file 'C:\\Users\\hello.py': [Errno 2] No such file or directory

# When give path to file it will work
C:\Users>python G:\div_code\hello.py
hello world
Reply
#9
(Nov-27-2022, 11:51 PM)snippsat Wrote:
(Nov-27-2022, 11:00 PM)jerryf Wrote: Here are the results of running the 2 cmds in your first reply :
Then Python Windows Path is ok✨
This mean that you can run python command from any folder,
of course the file most be in folder you are in,or have give to give path to file.
Example hello.py is not in Users folder then it don't work,in this case have to give path to file the it work.
C:\Users>python hello.py
python: can't open file 'C:\\Users\\hello.py': [Errno 2] No such file or directory

# When give path to file it will work
C:\Users>python G:\div_code\hello.py
hello world

Thanks again snippsat. So to confirm my understanding-when using cmd mode python does not appear to make any reference or use of Path and PYTHONPATH environment variables in searching for the script file, and so a full pathname must always be given unless starting from the directory containing the script file. Is that correct?
Reply
#10
(Nov-28-2022, 10:09 AM)jerryf Wrote:
(Nov-27-2022, 11:51 PM)snippsat Wrote: Then Python Windows Path is ok✨
This mean that you can run python command from any folder,
of course the file most be in folder you are in,or have give to give path to file.
Example hello.py is not in Users folder then it don't work,in this case have to give path to file the it work.
C:\Users>python hello.py
python: can't open file 'C:\\Users\\hello.py': [Errno 2] No such file or directory

# When give path to file it will work
C:\Users>python G:\div_code\hello.py
hello world

Thanks again snippsat. So to confirm my understanding-when using cmd mode python does not appear to make any reference or use of Path and PYTHONPATH environment variables in searching for the script file, and so a full pathname must always be given unless starting from the directory containing the script file. Is that correct?

So here is the nub of my confusion - see 2 examples below remembering that the script file is in directory C:\python311\my_python_files:

Example 1 - Script executes without explicitily invoking python or including full pathname
C:\Users\jerry>hello_world.py
Output:
Hello World
Example 2 - Script fails when explicitly invoking python
[python]C:\Users\jerry>python hello_world.py[/pyhon]
Error:
python: can't open file 'C:\\Users\\jerry\\hello_world.py': [Errno 2] No such file or directory
So not explicitly invoking python appears to not require the full pathname for the script file - as per Example 1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failure to run source command middlestudent 2 705 Sep-22-2023, 01:21 PM
Last Post: buran
  File path by adding various variables Mishal0488 2 1,038 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Dickey Fuller failure Led_Zeppelin 4 2,633 Sep-15-2022, 09:07 PM
Last Post: Led_Zeppelin
  Script Path tfernandes 8 2,208 Jul-07-2022, 09:48 PM
Last Post: tfernandes
  Simple Python script, path not defined dubinaone 3 2,699 Nov-06-2021, 07:36 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,215 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Assert failure jtcostel 1 1,646 Sep-03-2021, 05:28 PM
Last Post: buran
  Subprocess.Popen() not working when reading file path from csv file herwin 13 15,106 May-07-2021, 03:26 PM
Last Post: herwin
  Add file to sys.path permanently hcccs 5 8,419 Jan-31-2021, 11:26 AM
Last Post: hcccs
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,146 Aug-09-2020, 02:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020