Home > Back-end >  Why does my code run in the VS terminal but not from the py file?
Why does my code run in the VS terminal but not from the py file?

Time:11-09

Beginner here. I've just learned the basics of python using VS. I don't know why I get a syntax error in the VSCode text file but not on the terminal for the command. Any assistance helping me understand would be great, thank you. screenshot of vscode

Tried to install boto3 with pip.

CodePudding user response:

You cannot run shell commands from a python script.

This is the right way to do it. You can also use the subprocess module to do it.

import os

# In Linux
os.system("python3 -m pip install boto3")

# In Windows
os.system("py -m pip install boto3")

Although, it's not recommended installing packages inside your code.

You can use a requirements.txt file. Then you just need to run this command once in your terminal:

pip install -r requirements.txt

CodePudding user response:

py -m pip install boto3

Obviously, this does not conform to python syntax.

Usually we call it a command line.

We run it in the shell instead of python file.

Python files will be compiled and then run. This command line statement will not be compiled (As the wavy line in the file reminds, this is an error code). You can further learn Python syntax to learn more about this problem.

  • Related