-1

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.

  • You are having a shell command line in your code. I doubt it runs anywhere. – Klaus D. Nov 09 '22 at 06:22
  • Thats not the correct way to have the command inside a script. You can refer https://stackoverflow.com/questions/12332975/installing-python-module-within-code to see how to install packages within a script. – Hema Jayachandran Nov 09 '22 at 06:24
  • `py -m pip install boto3` is not valid Python syntax. You are trying to execute a shell command in Python. – juanpa.arrivillaga Nov 09 '22 at 06:25
  • @HemaJayachandran almost certainly, the OP shouldn't be installing a package in the script – juanpa.arrivillaga Nov 09 '22 at 06:25
  • Thanks everyone for their answers. I have now learned what shell commands are and everything is installed and working. I figured there was some python syntax I hadn't learned and that's why all these websites were asking me to run that line of code :P – Corey King Nov 16 '22 at 11:18

2 Answers2

0

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
ChamRun
  • 104
  • 6
0

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.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13