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,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- Word count in Notepad++ - NotepadPlusPlus
- Free Online Eraser Tool for Images (PNG, JPG, JPEG and GIF) - Tools
- Android : Unable to load VM from snapshot : Mac OS X Error - Android
- Ways to Increment a counter variable in Bash Script - Bash
- Base 64 Encoding Decoding In Notepad++ - NotepadPlusPlus
- jQuery : Move to top of the page - jQuery
- Mac - Chrome Open a new window in Incognito mode - Chrome
- [Solution] fatal: not a git repository (or any of the parent directories): .git - Git