首页 > 解决方案 > First time user of VS Code and Python

问题描述

First time user of Python in VS Code. My import is not being recognized, i.e. random?

I have run my code in Anaconda Jupyter, so I know my code works. It's a config issue.

import random
num = random.randint(1,2)

I expect no run-time errors.

But I get the following response in the console:

num = random.randint(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'random' is not defined

标签: pythonpython-3.xvisual-studio-code

解决方案


I think that you had not configured you python interpreter. You can select a Python interpreter by opening the Command Palette (Ctrl+Shift+P), start typing the Python: Select Interpreter command to search, then select the python interpreter you want to use for your current file.

If it still not working, you can refer to this VS Code document for more details.


推荐阅读