Avi
Avi

Reputation: 1845

How to Run .py file from Windows Command Prompt?

I am trying to run the file created for Selenium using Command Prompt but I am unable to figure out what is the problem as I followed some of the solutions provided here and in Google as well but I am receiving the same error message.

When I am trying to run this code one by one: I can run it without any error and login successfully

Here is the code:

  import selenium
  from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.ui import Select
  from selenium.common.exceptions import NoSuchElementException
  baseurl = "http://www.gcrit.com/build3/admin/"
  username = "admin"
  password = "admin@123"
  xpaths = {'usernameTxtBox': '//input[@name="username"]', 'passwordTxtBox': '//input[@name="password"]', 'loginButton': '//button[@id="tdb1"]'}
  mydriver = webdriver.Chrome(executable_path=r"C:\mypath\Forselenium\chromedriver.exe")
  mydriver.get(baseurl)
  mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)   
     mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)
  mydriver.find_element_by_xpath(xpaths['loginButton']).click()

I saved this in a .py file, and then added the extension of python37 in Environment variables "C:\mypath\Local\Programs\Python\Python37" and added .py in the path as well.

My python file is saved in other file location. So I tried running the file in different ways I ran in this way:

> C:\Users\mypath\Python37\python.exe "C:\Users\mypath\PythonScripts\SeleniumPractice.py"

I received this error message "SyntaxError: unexpected character after line continuation character"

Also, I tried to run in the way mentioned in this link how to run .py files in command prompt (Windows 7) but it is still not working properly.

  I tried some of the solutions provided below as well but I am receiving error for some reason: 
 I changed the directory as well, but it is saying no such file but the file (SeleniumPractice.py)  exists in this path. 
    C:\Users\Desktop\Learning\PythonScripts>py SeleniumPractice (tried using .py as well but receiving error like invalid syntax) 
   (null): can't open file 'SeleniumPractice': [Errno 2] No such file or directory

I tried using the other solution provided below as well, but receiving this error.

   I am using this command C:\Users\>python SeleniumPractice.py "File 
  "SeleniumPractice.py", line 1 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 
   2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 ^ SyntaxError: 
   invalid syntax"

Since I created scripts from IDLE, I had >>> on each line, which I replaced to blank and commented out any other rows generated from IDLE, and now I can run the script using the same solutions provided below python .py. Thank You Everyone

Upvotes: 0

Views: 2264

Answers (5)

barshopen
barshopen

Reputation: 1311

I think there's a difficulty with your question because you are giving a bundle of errors you are getting

  1. Note that as you wrote:

I received this error message "SyntaxError: unexpected character after line continuation character"

We can assume that you actually CAN run .py files from the command prompt. You just have syntax error. From reading your code, I see a redundant tab on the next to last line, that might give you the error.

2.You wrote

C:\Users\Desktop\Learning\PythonScripts>py SeleniumPractice (tried using .py as well but receiving error like invalid syntax) (null): can't open file 'SeleniumPractice': [Errno 2] No such file or directory

Meaning you get a path error. It is also a python error saying to you that it can't find in the current path you are at (i.e. C:\Users\Desktop\Learning\PythonScripts) the file "SeleniumPractice". Is it really located there?

  1. You wrote :

I am using this command C:\Users>python SeleniumPractice.py "File "SeleniumPractice.py", line 1 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 ^ SyntaxError: invalid syntax"

But we can't acutally know which Syntax Error you got. It only says you got a syntax error in line 1 of your code. Try maybe running it from IDLE and give us full description of your error?

And as written in one of the comments, pycharm might be really helpful for you understand better the mechanics of the errors you are getting. Or at list learn a little about how to work with Command Prompt, because, for example, if you install PATH correctly in environment variables, you don't need to write full path of you python.exe location, just need to write "python" and then the relative path of the file you are trying to run, and it will run beautifully.

Upvotes: 1

wndtanaka
wndtanaka

Reputation: 11

If you want to start running using command prompt, you might want to go to that directory where that file is, and type

python your_file.py

If you using Python 3.x you can change it to python3 instead.

Upvotes: 1

SocketPlayer
SocketPlayer

Reputation: 166

you have SyntaxError in your code.

that mean your code have some bugs that forbid it from running

also you can run your code by:

python <path_to_code_file>

or

py -3 <path_to_code_file>

בהצלחה

Upvotes: 0

albusSimba
albusSimba

Reputation: 469

You seem quite new to python I would recommend using an IDE like pycharm. It works like a charm

Upvotes: 1

katamit
katamit

Reputation: 76

go that location in your command prompt then

python <filename.py>

you can check if python is being recognized as internal command by windows by simply executing the

python

command in your command prompt. it give an error then the environment variable setting is not correct but usually that is not the case now with new version of python as it automatically ; i.e. at installation itself creates the entry.

Upvotes: 1

Related Questions