How can I execute python scripts from another directory - 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.

Related

Trying to run Python program from command prompt

Path must be messed up and I can't fix it.
In the command prompt I am trying to open and run a python program that I made in IDLE. I am running Python 3.8.5. According to Automate the Boring Stuff, I should just be able to do:
py birthdays.py
But when I do that I get:
C:\Users\name\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file 'birtdays.py': [Errno 2] No such file or directory
All of my scripts are located in: C:\Users\henri\AppData\Local\Programs\Python\Python38-32.
The only thing that I could think of is that it is going one step too far and searching in python.exe instead of just Python38-32. But Ii'm not even sure it works like that.
I tried to add the exact path using:
set PATH=%PATH%;C:\Users\name\AppData\Local\Programs\Python\Python38-32
But when I do this and press enter nothing happens.
when I want to use CMD to run python scripts, I just use
cd\
to back the main root of drive C. then use
cd folderAddress
for several time until to reach the exact scrip containing folder. Finally, I type
python scriptName.py
In your command prompt type python.The output should be python 3.** (depending on your python version).
If you see the python version it's working fine. In command prompt navigate to the folder that you have your python file using cmd command. then type
python birtdays.py
Don't forget the space after python.

Can't run Python file via Command Prompt, File is not defined error

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.

How can I run Python interpreter in Windows Powershell ISE?

I am a beginner at Python and I am trying to run the Python interpreter from the command line in Windows Powershell ISE.
But here is what I got when I type in python or py:
Instead of
>>>
I got the following (with a line break inbetween):
>
>>
Then the commend line prompt is locked and not responding.
Any help is appreciated!
Are you trying to run a python file (.py) from the command line or run a line of python like 'print('Hello World')'?
If you are trying to run the actual .py file then you will need make sure you are in the same directory of the file then run "python someFile.py" from the command prompt.
If you are trying to just run a function like print('Hello World') then when you are in command prompt and enter "python" you will see the '>>>' as just place holders so you wont be able to erase those. You should now be able to run print('Hello World') and see a result.
Which version of Python are you using and on what platform are you working? One of the best options to start learning Python is Anaconda. Then run Anaconda Prompt and there you can type python and see ">>"!

Permission Denied when executing python file in linux

I am working with my Raspberry Pi 2 B+ and I am using Raspbian. I have a python script located at /home/pi/Desktop/control/gpio.py
When I type /home/pi/Desktop/control/gpio.py into the command line, I get the message
bash: /home/pi/Desktop/control/gpio.py Permission denied
I have tried running sudo -s before running that command also but that doesnt work. My python script is using the Rpi.GPIO library.
If someone could please explain why I am getting this error it would be appreciated!
You will get this error because you do not have the execute permission on your file. There are two ways to solve it:
Not executing the file in the first place. By running python gpio.py python will load the file by reading it, so you don't need to have execute permission.
Granting yourself execute permission. You do this by running chmod u+x yourfile.py.
However, doing so will not work unless you add a shebang at the top of your python program. It will let your linux know which interpreter it should start. For instance:
#!/usr/bin/env python
This would try to run python using your current $PATH settings. If you know which python you want, put it here instead.
#!/usr/bin/python3
Remember the shebang must be the very first line of your program.
do like this maybe work:
cd /home/pi/Desktop/control/
python gpio.py
Because gpio.py is not a executable file, you should run it by python instead

Invalid syntax when trying to open file path?

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...

Categories