0

I have a python file that needs a virtual environment to be activated in order to be run, and I want this file to be run whenever I start my PC. Problem is a terminal window opens even though I'm using pythonw. It is just a blank terminal window.

So here is what I did, I created a bat file that looks like this d:\folder\venv\Scripts\pythonw.exe d:\folder\thefile.py this does not fully work because even though it does not open a terminal window for the python file it does open a blank terminal window that I have to close manually. I want to get rid of this. Finally I thought I can put the bat file here C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

rpicoder
  • 11
  • 2
  • You don't want, or need, a batch file. Create a shortcut and place it in your `Startup` directory instead. A batch file runs in `cmd.exe`, hence why the window is opening. BTW, had you used ```@start d:\folder\venv\Scripts\pythonw.exe d:\folder\thefile.py``` you wouldn't have had to close the `cmd.exe` window. – Compo Aug 17 '23 at 08:07
  • @Compo shortcut for what exactly? the python file? how will I activate the virtual environment if I directly add the shortcut to the python file? Also, using start works thank you – rpicoder Aug 17 '23 at 08:18
  • How are you activating it now? You can't be doing it with the batch file, because I was very careful to read your question, which stated, "I created a bat file that looks like this d:\folder\venv\Scripts\pythonw.exe d:\folder\thefile.py". – Compo Aug 17 '23 at 08:19
  • actually, with a batch file you don't need to activate the virtual environment. Just running the python.exe in the virtual environment directory is enough. d:\folder\venv\Scripts\pythonw.exe [explained here](https://stackoverflow.com/questions/47425520/activate-virtualenv-and-run-py-script-from-bat) – rpicoder Aug 17 '23 at 08:20
  • You asked how, not me! "how will I activate the virtual environment if I directly add the shortcut to the python file?" – Compo Aug 17 '23 at 08:29
  • you asked me to use a shortcut instead of a batch file. I know how it is done with a batch file, apart from the small issue I had, which you helped me with so thank you for that, but I don't know how to use a shortcut to achieve the same, which is why I asked – rpicoder Aug 17 '23 at 11:03
  • Did you try, Target: `pythonw.exe ..\..\thefile.py` Start in: `D:\folder\venv\Scripts`? – Compo Aug 17 '23 at 11:32

1 Answers1

0

if you have a problem with opening a terminal and you want to execute your Python app without seeing it, simply put the prefix of your file to MyPythonAppName.pyw

this will make your app run a terminal in the background without seeing the terminal.

but be aware the scenario will be different if you wanna convert it to .exe in that case, you should run pyinstaller with --noconsole switch.

jexroid
  • 11
  • 4
  • the .pyw only makes sure that it is not python.exe that runs the file but pythonw (by default) right? here I'm making sure that pythonw is being run so that means I don't need to change from .py to .pyw. I still did try changing my file to .pyw, however, it still opens the blank terminal – rpicoder Aug 17 '23 at 08:06
  • i didn't fully understand your problem, you have a while loop in your code that causes the blank terminal? and no, .pyw will run by python.exe at any condition. but if you want to get rid of the console, the .pyw is the best option. https://stackoverflow.com/questions/34739315/pyw-files-in-python-program – jexroid Aug 18 '23 at 06:44