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"])'

Fix: import: command not found
There are two ways to look at this issue,
Run the file as a Python Program
In most cases, one might be trying to run the .py file not as a script but as a Python program, so use python command instead of running it as a script.
Example:python3 script.py
sample.py
data_folder
Run the file as a Python Script
If you want to run the file as a Python Script, then make sure that the first line of your .py file is a shebang,
#!/usr/bin/env python3
import sys
import subprocess
output = subprocess.check_output(["ls"])
print(output.decode())
./sample.py
sample.py
data_folder
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- How to Escape a character in a Bash command String - Bash
- How to clear cache and browsing history in Google Chrome? - Chrome
- Convert String to int in Java - Java
- How to Mount Google Drive in Colab Notebook - Google
- How to install Java OpenJDK 11 on Alpine Linux - Java
- How to create Custom RatingBar Android Programming Tutorial - Android
- 86 Gmail keyboard shortcuts that you may find Advantageous - Google
- Android appcompat_v7 Error retrieving parent for item: No resource found that matches the given name - Android