1

I have tried nearly all solutions in my mind to do this, but the fact is I can't run a python script with modules imported already.

Here's my code for the module cls.py:

import os
def cls(): 
     os.system('cls')

Given below is the code for opening python in cmd:

@echo off
python
pause

What I need is to open the python in the command prompt with the module cls imported. Also, when I try python -m <module> way, it doesn't show any errors, but the program ends.

Any help would be greatly appreciated and thanks in advance. Saw this question related to mine, but it is not my problem: Run python script that imports modules with batch file

2
  • I don't really understand your question. What is the batch file supposed to do? It just starts the interactive Python interpreter, is that what you want? Or do you want to execute the Python file src.py? In that case just run python cls.py. Commented Nov 12, 2021 at 12:43
  • @wovano the batch file will start python in the command prompt. I require this to work on the interactive shell of python. python cls.py would simply open the program and would finish instantly without taking further commands. Commented Nov 12, 2021 at 12:48

1 Answer 1

1

I think what you'r looking for is the interactive mode:

-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command

So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.

Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:

If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.

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

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.