Windows 10 Home Version 1607. Python 3.5.2
I'm new to python, I've tried to run python script using a batch file. I've added my folder contain batch files to the PATH Environment Variables. Have tried to run my python script by typing in 'HelloWorld' on Win+R
However a error message pops saying 'This app can't run on your pc' and the cmd keeps on returning the current message
Access Denied. Press any key to continue.....
Python Script. Saved as HelloWorld.py
#! python3
import sys
print('Hello World')
print(sys.argv)
Batch File. Saved as HelloWorld.bat
#py C:\Users\Anthony\MyPythonScripts\HelloWorld.py %*
#pause
You are mistakenly using py instead of python to execute your script.
This causes the messages you are getting.
If you use python 3.5.2 or python 2.7 in a project or in a script to run it you should use the command
d:\Users\nameofuser>python nameofmyscript.
For exemple i have a script in python2:
import os
print "Hello wolrd! I'm a rabbit!"
i will run it with python rabbit.py
answer: Hello wolrd! I'm a rabbit!
if i have my script in python 3
import os
print("Hello wolrd! I'm a rabbit!")
this time i can use python3 rabbit.py or python rabbit.py
The only thing you can't do is to use python3 for a python 2.7 script you will have a SyntaxError
Related
I got a Node.js CLI program called meyda installed (Mac OS 10.14) using:
sudo npm install --global meyda
From the Terminal I can call the program and it works as expected; like:
meyda --bs=256 --o=apagodis2.csv DczN6842.wav rms
Now, I want to call it from inside a python script (using Spyder) at the same location and tried this – but getting error:
import os
os.system ('/usr/local/bin/meyda --bs=256 --o=apagodis4.csv samples_training/DczN6842.wav rms')
>>> env: node: No such file or directory
I can issue more "traditional" shell commands like this from the same Python script and it works:
os.system ('cp samples_training/DczN6842.wav copy.wav')
Also tried subprocess call with same result. I confirmed the executable is at /usr/local/bin/
To make sure I also removed all file arguments calling the program using only the help flag but same, error.
os.system ('/usr/local/bin/meyda -h')
>>> env: node: No such file or directory
Why is the command not found from inside Python but sucessfully in the macOS Terminal?
I'm currently working on Pycharm with remote python Interpreter(miniconda3/bin/python).
So when I type echo $PATH in remote server, it prints
/home/woosung/bin:/home/woosung/.local/bin:/home/woosung/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
I created project in Pycharm and set remote python Interpreter as miniconda3 python, it works well when I just run some *.py files.
But when I typed some os.system() lines, weird things happened.
For instance, in test.py from Pycharm project
import os
os.system('echo $PATH')
os.system('python --version')
Output is
ssh://woosung#xxx.xxx.xxx.xxx:xx/home/woosung/miniconda3/bin/python -u /tmp/pycharm_project_203/test.py
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Python 2.7.12
Process finished with exit code 0
I tried same command in remote server,
woosung#test-pc:~$ echo $PATH
/home/woosung/bin:/home/woosung/.local/bin:/home/woosung/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
woosung#test-pc:~$ python --version
Python 3.6.6 :: Anaconda, Inc.
PATH and the version of python are totally different! How can I fix this?
I've already tried add os.system('export PATH="$PATH:$HOME/miniconda3/bin"') to test.py. But it still gives same $PATH.(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games)
EDIT
Thanks to the comment of #Dietrich Epp, I successfully add interpreter path to the shell $PATH.
(os.environ["PATH"] += ":/home/woosung/miniconda3/bin")
But I stuck the more basic problem. When I add the path and execute command the some *.py file including import library which is only in miniconda3, the shell gives ImportError.
For instance, in test.py
import matplotlib
os.environ["PATH"] += ":/home/woosung/miniconda3/bin"
os.system("python import_test.py")
and import_test.py
import matplotlib
And when I run test.py,
Traceback (most recent call last):
File "import_test.py", line 1, in <module>
import matplotlib
ImportError: No module named matplotlib
Looks like the shell doesn't understand how to utilize modified $PATH.
I find the solution.
It is not direct but quite simple.
I changed os.system("python import_test.py") to os.system(sys.executable + ' import_test.py').
This makes the shell uses the Pycharm remote interpreter(miniconda3), not original.
I am trying out the sys library in python. In command prompt I am using this.
>>>import sys
>>>sys.ps1 ='$'
#my own input 'print 'test print''
$print 'test print'
test print
That worked in CLI, however, when I tried to do it in a python file and run it in CLI ( python file.py did not return anything.
#!/usr/bin/python
import sys
sys.ps1= '$'
Am I missing anything in the file?
What are you trying to do? sys.ps1 will just set the prompt for the python interpreter, so as soon as it exits it's effect is gone again. When running a file, there is no command-line.
If you want to see this have an effect, try running this with python -i file.py - this will run the command, and then drop you into the interactive shell, which should have PS1 set to $ now
I am using python and trying to execute a system command as below
code.py
import commands
import os
os.system('updatedb')
result:
sh-4.2$ python code.py
updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'
So how to execute all the system commands like above from a python module ?
This is almost certainly simply a permissions problem.
If you can trust your script to run as root:
$ sudo python code.py
I was about to test the ftpmirror builtin script (python322, winXP 32bits) from the cmd windows default shell and get this :
File "C:\Program Files\python322\Tools\Scripts\ftpmirror.py", line 161
print('Skip pattern', repr(pat), end=' ')
^
SyntaxError: invalid syntax
I tested the print() line directly in the python shell, trough cmd, and with idle (and in blender also) : this work obsiously.
I reproduce the error with a coucou.py file like this :
#! /usr/bin/env python3
pat = 'toto'
print("Skip pattern", repr(pat), end=" ")
when directly called from a cmd prompt :
C:\Program Files\python322\Tools\Scripts>coucou.py
same error than with ftpmirror
but :
C:\Program Files\python322\Tools\Scripts>python coucou.py
is ok
and my environment is ok I can execute py scripts directly from the windows ui by double-clicking a .py file, and I got working scripts working fine when called from .bat
I don't get it, it looks specific to the print() end argument, what did I not read yet about the way to execute python3 from the windows cmd shell ?
thanks,
Jerome
Try checking if you are running the same python interpreter when you double click or you run python from the command-line.
Save this in a .py file with this content and try running it with both methods:
import sys
print sys.version_info
I bet you are using different interpreters in each case.