Fix: line 1: import: command not found Python


If you are trying to execute a .py Python file in the terminal and you get the error "line 1: import: command not found" then the most common reason for it is that you are trying to execute the Python file as a script and it does not have the shebang line.

Example Code:
import sys
import subprocess

output = subprocess.check_output(["ls"])
print(output.decode())
Error:
Code2care@Mac % ./sample.py 
./sample.py: line 1: import: command not found
./sample.py: line 2: import: command not found
./sample.py: line 4: syntax error near unexpected token `('
./sample.py: line 4: `output = subprocess.check_output(["ls"])'
line 1 - import - command not found

Fix: import: command not found

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap