0

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.

0

3 Answers 3

1

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?

Sign up to request clarification or add additional context in comments.

Comments

1

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()

2 Comments

Would I type the latter into a separate file or into the terminal?
Both would work. Actually my bad I updated the answer.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.