Home > Mobile >  why doesnt my .py file work in windows but works perfectly fine in the PyCharm IDE
why doesnt my .py file work in windows but works perfectly fine in the PyCharm IDE

Time:01-02

im a new programmer and i've written a simple code to close my friends PC. Ive got the code working in the idea but when ever i tried to open in in the :D/ file it never works. the code has afew imported packages which i think are the problem but cant be sure. ive tried to search it up in youtube and ive tried running it from the command prompt but no use. What should i do

import pyautogui as pyautogui
import random
import keyboard
import time

def auto():
    pyautogui.moveTo(10,1070,3)
    pyautogui.leftClick()
    pyautogui.move(0,-50)
    pyautogui.leftClick()
    pyautogui.move(0,-100)
    pyautogui.doubleClick()

for i in range(1):
    auto()

ive tried search it up in youtube to find a solution and ive also tried opening in from the command prompt. idk what else should i have done

CodePudding user response:

You are probably not using the same Python environment that you are using with PyCharm (and where you installed pyautogui).

Pycharm activates that environment automatically, so you should do the same when you run your command.

You can use python.exe from your virtual-environment to run your .py file and then it should work fine.

PS. there are also a couple of things out of place on your code: you can remove the range(1) and the as pyautogui for example.

CodePudding user response:

I find windows very difficult to work with because of the environment variables and registry settings needed to automatically open a file with a given program. (Especially as I keep upgrading python as new versions are released)

I have settled on creating a folder to work in for each project with a batch file, called setpython.bat like this:

@echo off
path = C:\Python310

(Or the relevant path to the requisite python installation)

Then I open cmd and navigate to this folder and run: setpython

Now I can run any of my python programs there with: python <file>.py

  • Related