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
I think there's a difficulty with your question because you are giving a bundle of errors you are getting
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?
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.
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.
You seem quite new to python I would recommend using an IDE like pycharm. It works like a charm
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.
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>
בהצלחה
Related
I am currently transitioning from running a python script just within a PyCharm anaconda environment to running it on a Linux (ubuntu) SFTP. Within this SFTP I've built the job file with all required packages and all modules seem to be installed properly.
I am having problems with random string syntax errors that did not exist before.
E.g this line
regFile = (f'{i}_{df.index[0].strftime("%Y%m%d")}-{df.index[-1].strftime("%Y%m%d")}_Model.gz')
Runs perfectly fine with no errors on Windows Python.
But when I run it within this Linux supercomputer, I get a syntax error on most strings, but nothing on the rest of the code.
File "./test.py", line 355
regFile = (f'{i}_{df.index[0].strftime("%Y%m%d")}-{df.index[-1].strftime("%Y%m%d")}_Model.gz')
^
SyntaxError: invalid syntax
Is this common to experience syntax errors transitioning between Windows and Linux? Any suggestions to fix this line of code to work for both?
Thanks!
Have tried ensuring all modules were downloaded properly in case it was a version issue with Python. Have unsuccessfully attempted switching the line slightly to work for both.
I've installed Python 3.10.5 on my Windows 10 machine. Works splendidly running a file by writing "python file.py". What I can't do though, is to get the extension code runner to run python files through the hotkey ctrl + b. I've reinstalled Python (made sure to check PATH). Rebooted the computer and reinstalled code runner.
If I change no settings I get this error:
"/usr/bin/python3: The term '/usr/bin/python3' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
I found this similar thread: Code Runner does not work for Python in VsCode
After changing the code in settings.json to
"code-runner.executorMap": {
"python": "\"$pythonPath\" $fullFileName",
}
I get the error:
1 | … Data\Local\Programs\Python\Python310\python.exe"
"c:\lekspel\test.py"
| ~~~~~~~~~~~~~~~~~~~~
| Unexpected token '"c:\lekspel\test.py"' in expression or statement.
adding in a "-u" returns this error:
Unexpected token '-u' in expression or statement.
Anyone got a clue how to sort this out?
I solved it by doing a fresh install of vscode, deleting my settings in roaming. Not an optimal solution by any means, but got it out of the way at least.
got the same error. it was because i added "py" to path during installation setup (check by typing "py" in terminal if it opens python) so coderunner wasn't able to run python with "python" keyword, anyway try the following solution and tell me if it works:
1.Go to extensions
2.Right click -> extension settings
3.Edit settings in Json for any option
settings of coderunner
4.change "python -u" to "py -u"
like this
5.save json file with crl+s.
Run code again
tell me if it works for you.
here mine worked like this
I recently installed both Python and Visual Studio Code. After taking an intro class I wrote a basic script and ran it in Visual Studio Code. That's when I noticed a problem with the way Python is setup in my Visual Studio code.
Problem:
When I start Visual Studio Code and open a python file, the Terminal defaults to "C:\Users\my_name\Documents Python" (this is the folder my python files are stored in). From what I understand, when you're in Python, the prompt should be ">>>". I am able to run my script but i cannot run any other Python code (ie something as simple as z = 5) in the terminal. If I type in "Python", I am prompted with ">>>" but can no longer run my script.
I thought this was an installation issue so i uninstalled and reinstalled both Python and Visual but the problem persists.
I tried adding the Python file path to where the program is installed to the windows environment under system settings and also clicked "Add to Path" when reinstalling Python but none of these solutions seemed to work.
when basic python code (ie z=5) doesn't seem to work but the script runs fine I get the error message below:
PS C:\Users\my_name\Documents\Python 2> z=5
z=5 : The term 'z=5' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ z=5
+ ~~~
+ CategoryInfo : ObjectNotFound: (z=5:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Below is the error message I get after I switch to python and try running my script
PS C:\Users\my_name\Documents\Python 2> python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> & C:/Users/my_name/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/my_name/Documents/Python 2/new2.py"
File "<stdin>", line 1
& C:/Users/my_name/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/my_name/Documents/Python 2/new2.py"
^
SyntaxError: invalid syntax
>>>
There is nothing wrong with your setup. It looks you miss basic understanding of different ways to execute python code.
VS Code has integrated terminal. From there You can run your python script i.e. file with py extension same as you are on terminal/cmd/powershell. That is common way to write and execute code.
When you type python and hit enter you start python interactive shell. That is when you get >>> prompt. Your python interpreter evaluate and execute each line as you type in and hit enter. Same will happen if you type in python in cmd/powershell outside VS Code. Interactive mode is used more or less to experiment, test simple ideas, simple code examples, etc. but the code you type in is lost once you exit the interactive mode by >>>exit().
for further reference you may check
VSCode from start
How to execute python code
After reading the documentation above, I also had a similar problem after running scripts. I think that VSC is exiting out of python after running a script and requires starting up the python terminal again. I wonder if VSC can default to python after running a script instead of exiting out to powershell.
I figured out what the issue was here. Visual code thought Python was installed in the directory that my .py files are saved in as opposed to the actual location under program files. The path had to be edited under settings.
When you are in debug mode and stopped, you can type interactive Python in the 'Debug Window'. Useful for inspecting objects, variables, etc.
Otherwise as suggested, I just type "Python" in the terminal to get the interactive Python prompt ('>>>').
I am trying to follow a bad instructions website and because of that i'm getting troubles in executing scripts.
The website URL:
http://www.tripleoxygen.net/wp/2014/01/sagemcom-modem-fst-2764-gv-power-box-gvt-desbloqueio/
The following commands is the ones i'm getting trouble with:
./unlocker.py --mode=install
The steps i'm doing:
Open CMD
D:(my flash drive directory)
python
unlocker.py --mode=install
What I get:
File "<stdin>", line 1
SyntaxError: can't assign to operator
but I don't think i'm executing the script, because there is no stdin in the file...
So what I'm asking is:
Are my steps wrong?
How can I execute the script correctly from another directory?
Thanks in advance.
That website doesn't say to start python then enter unlocker.py. It says to type ./unlocker.py from the command prompt, not the Python prompt. You may need to do python unlocker.py, but again, you're doing that from the command prompt.
I'm trying to learn python but have some problem running source files from power shell. When I type 'python' it opens up and I can type python commands directly in the shell. I think this is called interactive mode. But when I try to run/execute a source file I get an error message: It sayys: Syntax error: invalid syntax.
I use 'python myfile.py' when I try to execute the script.
If I run the same file from IDLE it works just fine. Ana idea what I'm doing wrong?
Here is myfile.py. I'm running python 2.7
# filename: myfile.py
while True:
s = raw_input('Enter something: ')
if s == 'Quit':
break
print 'Lenght of the string is', len(s)
print 'Done'
You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type >python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g >python C:\myfile.py.
If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g. >myfile.py
I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just >myfile should work.
Edit after Update:
Typing just >python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.
You will need to put the full path of the Python executable within the command line in order for it to work. You could check and ensure that your python exe is included in your Path among your system variables.
Disclaimer: I don't know PowerShell, but I do know cmd.exe.
I don't know why python myfile.py doesn't work, but assuming that PowerShell bears at least some similarity to cmd.exe, the following should probably work: myfile.py. That's right, just enter the name of the Python script and hit enter.
If you started by typing "python" in powershell you will need to get out of that script.
If you are in python type:
quit()
then type
python myfile.py
This should work if your python is installed correctly.
Try to type this in Powershell:
$env:path="$env:Path;C:\Python33
After this, command
python yourfile.py
should work.
This my sound silly, especially coming from a beginner.
Just save the file on your desktop. Open up powershell and drag the file directly into powershell and it opens. kind of tedious but it works