Governor
Governor

Reputation: 312

Using the python shell after running a program in VSCODE

Lets say I have a program which just has a class, I want to be able to create instances of the class in the shell and use the methods that the class has without having to write them into the file.

Obviously I can do this in IDLE but how do I do it in VSCODE?

The closest I can get is the start REPL which gives a Python Shell without having the file run as well.

Upvotes: 0

Views: 448

Answers (3)

Molly Wang-MSFT
Molly Wang-MSFT

Reputation: 9521

Type python in integrated Terminal to enter Interactive mode, then import filename and call function as the following steps:

enter image description here

Press Ctrl+Z to quit interactive mode.

Upvotes: 1

niaei
niaei

Reputation: 2419

You can import them.

Let's say you have a file named my_lib.py

my_lib.py

class MyClass:
    pass

class MyClass2:
    pass

You can open a terminal and change working directory to where my_lib.py exists and run python. Then:

>>> import my_lib
>>> 
>>> mc = my_lib.MyClass()
>>> mc2 = my_lib.MyClass2()

Upvotes: 1

cavalcantelucas
cavalcantelucas

Reputation: 1382

You can import the class from that file. Let's say your class is named MyClass and it is in file myfile.py. You can do something like this in your myscript.py:

# myscript.py
from myfile import MyClass

Make sure to create an __init__.py file in the root.

Then you can run

$python -m myscript

Can you share more details?

Upvotes: 1

Related Questions