I cant seem to figure out why I get and invalid syntax error when trying to open my downloads folder? Here's what I'm typing into a python shell:
cd c:\users\ryan\downloads
I get the error:
SyntaxError: invalid syntax
and it highlights the "c" in "c:\"
any suggestions?
Thanks,
cd is a shell command, not a Python statement. It should be run in a shell or command interpreter, not in the Python REPL.
cd whatever is a fine command for e.g cmd.exe, but not for python.exe even when run as an interactive read-eval-print loop.
import os followed by a call to os.chdir(whatever) is more likely to do what you what within an interactive Python interpreter...
Related
I was trying to run a python file "exe.py" on vs code by typing:
python3 .\exe.py
But it comes with the following replys from vs code:
python3: can't open file '/Users/exercise/my-python-app/myenv/.exe.py': [Errno 2] No such file or directory
No ideas why couldn't it works given that I already installed the python package and running the command in appropriate environment.
You are running
python3 .\exe.py
which is escaping the e and therefore is replaced by e as there's no matching escape char and it becomes
python3 .exe.py
You need, as other response states
python3 exe.py
or
python3 ./exe.py
In fact python3 is installed in your system but you are typing incorrectly the command. Imagine that you have the python file named exe.py with the following code
print("Hello world")
If you are in the same directory of the file you can execute it using the command
python3 exe.py
Another way to execute exe.py is using the shebang line. The shebang is a line that you can write to tell to the operative system which executable must use to interpret the code of the file. If you add the typical shebang for python3 in linux at the beginning of exe.py
#!/usr/bin/env python3
print("Hello world")
and you tip the command
./exe.py
you will see the result of the execution too. If you get an error probably you have to give permissions to the file with the command
chmod 777 exe.py
You can find a lot of resources talking about the shebang. The following link talks about the shebang in python
https://www.pythonpool.com/python-shebang/
I hope you found it useful.
Greetings.
I am new to Python and currently doing a basic python course to learn. I have been running code all day via the Command Prompt and it has been working fine. For some reason though it has stopped working and python files I try to run are returning the following error:
Traceback (most recent call last):
File "", line 1, in
NameError: name 'hello' is not defined
As per the screen shot and the numbers on it, I performed the following steps in an attempt to run the file:
change to the folder where file is saved
run 'dir' to list all files. I am trying to run 'hello.py'. This contains the code: print('hello simon!')
I can run the file by just typing hello.py into the command prompt, this works ok
I can also run with: python hello.py - this works ok
when I activate Python by typing Python --> Enter, this starts the interpreter ok. However if I then try to run by typing hello.py I get the error message.
This has worked ok all day, I have not changed anything on my PC (to the best of my knowledge!) but just started to get this error a few hours ago. I have looked all over the internet for solution but found nothing. I have uninstalled and re-installed Python, restarted etc... all to no avail.
I am running Python 3.6.5 on a Windows 7 64 bit PC.
It won't let me attach a picture so here is link to screenshot of Command Prompt and error: https://i.stack.imgur.com/BBUe5.jpg
I hope someone can help me with this please
Thankyou
You are not supposed to execute hello.py in the Python Interpreter. It won't work. When you type in python and hit Enter in your Command Prompt, Just type this,
>>> print('hello simon!')
And hit Enter, it would definitely work. Because the interpreter is supposed to execute a code line by line. So if you want to run a Python Script then do not execute it in the Interpreter.
The problem is that when you write python (alone), the command line calls python shell and the following commands are run inside the python shell and not in the command line anymore. Calling a script from the shell has a different format (look it up). You can enter exit() to exit the shell back to command line again
What you are trying to achieve is you are running Hello.py inside Python.
Not with Python.
You need to run Hello.py with Python. As python is interpreter over here.
>>>python
means you are going inside python shell
>>>print('hello simon!')
Is equivalent to your program.
You are running your Python Script as you should and it's working. If you added Python to your path you can run Script you only need to call the Script "hello.py". If you have more than one intepreter, or you didn't added it to your path then you can call it like this "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" you can enven pass arguments to it "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" --argument
When you type python in a shell, then interactive mode is activated. This is like a shell where you type commands and got interpreted right away, in the same way as cmd and powershell works, but for python. This way you can test snippets, or just do simple stuff overly complicated like this
import os
ls = os.listdir(os.path.abspath('c:/'))
def print_dir():
for file in ls:
print(file)
Wich in cmd would be dir c:\ or in powershell ls c:\. The point is that you can test libraries, explore objects, replace the shell or just have fun.
okay, im a new guy at all this, just randomly picked it up with my neighbor and we are both stuck at this. We have been following this tutorial(here) and have made it to 6.6 in the tutorial. I have searched the forums looking for a way to get passed my problem but all the of questions people have are too complex for me as of right now. I am running windows 8.1 on my laptop, i have python27. So here we go i put in,
>>> cd c:\\py
and i get
File "<stdin>", line 1
cd c:\\py
^
SyntaxError: invalid syntax
Then i searched around and found a thread saying to use os.chdir so i gave that a shot and got;
>>> os.chdir("c:\\py")
>>> os.getcwd()
'C:\\py'
>>>
So my guess is that it worked? so then i go ahead and try to run my program like it says to do, so i put in
python hello.py
and i get this in return
>>> python hello.py
File "<stdin>", line 1
python hello.py
^
SyntaxError: invalid syntax
I'm literally stuck, i have no clue what to do now. If someone can help me through this i will love you long time.
Thank you
First of all, Python shell differs from system shell (cmd.exe). You try to run python script.py in Python interpreter instead of cmd.exe.
Open cmd.exe and type in python script.py to solve this. It'll run fine if it doesn't contain any errors. cd c:\\ doesn't work due to the same reason.
First quit() or exit() the Python interpreter (type one of them right in it) then type the commands you want to execute (such as cd) into terminal.
If you want to run code.py in Python interpreter, you can os.chdir("...") to the directory where your script resides and type import code. That may not work if your script contains
if __name__=="__main__":
All in all, Python interpreter is for running Python code right in it and command prompt (terminal, cmd.exe) is for running other non-GUI programs and much more.
You are in the python interpreter which is an interactive shell. You can consider it "scratch paper" to test out or try different things.
To run your script :
quit()
in the command prompt run python.exe hello.py ( on windows.. on *nix just python)
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