I am working on MacOS to learn the environment
#map_it.py - launches a map in the browser using an address from the command line or clipborad
#!/usr/bin/env python
import webbrowser, sys
if len(sys.argv) > 1:
#Get the address from the command line
address = "".join(sys.argv[1:])
print(address)
#todo:get the address from the clipboard
I set shebang at top of the script which did not work as I intended
$ map_it.py test
-bash: map_it.py: command not found
$ map_it test
-bash: map_it: command not found
After I changed #!/usr/bin/env python to #!/usr/bin/env python3 and #!/usr/bin/env,
It report the identical error.
How could I fix such a bug?
You need to do either:
$ ./map_it.py test
or
$ python map_it.py test
...otherwise you are indicating the script is a system installed command.
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 am trying to run below code in jenkins job, code is to delete files older than 30 days from a directory in ftp server.
I created freestyle project job in jenkins and in build section I have selected "Execute shell" and I have added below code.
#! /usr/bin/python
import time
import ftputil
host = ftputil.FTPHost('host', 'user', 'pass')
mypath = '/path/directory'
now = time.time()
host.chdir(mypath)
names = host.listdir(host.curdir)
for name in names:
if host.path.isfile(name):
host.remove(name)
host.close()
I am facing below error on build
Building remotely on docker-4 (maven linux docker) in workspace /var/lib/jenkins/workspace/Capacity/folder/Test_job
[Test_job] $ /usr/bin/python /tmp/jenkins8422988908580909797.sh
File "/tmp/jenkins8422988908580909797.sh", line 6
SyntaxError: Non-ASCII character '\xc2' in file /tmp/jenkins8422988908580909797.sh on line 6, but no encoding declared;
and I also tried with "Execute python script" build option I am facing similar error like below.
Building remotely on docker-4 (maven linux docker) in workspace /var/lib/jenkins/workspace/Capacity/folder/Test_job
[Test_job] $ python /tmp/jenkins5375363980435767190.py
File "/tmp/jenkins5375363980435767190.py", line 6
SyntaxError: Non-ASCII character '\xc2' in file /tmp/jenkins5375363980435767190.py on line 6, but no encoding declared;
I am new to jenkins job and python, can any one guide how can I resolve this issue.
2) If I select jenkins pipeline job how can I call this python code from jenkinsfile.
Try to install ftputil globally.
pip install ftputil
or
sudo pip install ftputil
The following shell script works with no error, once I installed ftputil globally with sudo.
#!/usr/bin/env python
import time
import datetime
import ftputil
There is nothing special about this python program. If you want to execute it from shell. Then create shell script file e.g. python_prog.sh then, change permissions chmod +x python_prog.sh python_prog.py
python_prog.sh
#!/bin/sh
python python_prog.py
Finally, run the script from terminal . python_prog.sh or ./python_prog.sh
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 to get tab completion to work while running pdb on OS X 10.10.5. I have installed the homebrew version of python 2.7.13 because it appears (also see this) that Apple does not ship with a functional readline. If I have a trivial script, trivial.py
var1 = "this"
var2 = "is annoying"
and I run /usr/local/bin/python -m pdb trivial.py and on the first entry I enter import readline, rlcompleter; I subsequently can get tab completion. However, if I put in my .pdbrc
import readline
import rlcompleter
tab completion does not work. How is this not the exact same thing? Shouldn't tab completion work when put in my .pdbrc?
I get the same behavior on linux.
ie. with no .pdbrc
$ python3 -m pdb foo.py
(Pdb) in<tab> gives interact
(Pdb) interact
(Pdb) import rlcompleter
(Pdb) in<tab>
(Pdb) in input( int(
If I have import rlcompleter in my .pdbrc I only get interact when I type in. I get this same result even after I import rlcompleter.
$ python3 -m pdb -c 'import rlcompleter' foo.py
Also prevents tab completion.
Comparing the output of
$ python3 -vv -m pdb -c 'import rlcompleter' foo.py
and
$ python3 -vv -m pdb foo.py
resulted in a segfault so I would consider this a bug. I suggest you file a bug. Mention something about the import rlcompleter could be missing the Pdb completeionkey=setting being overwritten or the cmd module could be misinitialized. FWIW here is the source I was looking at to gleam some additional info. Pdb source
I found this
Using this method I was able to get the tab completion to work. His code uses a .pdbrc in the source dir and a hidden python script in the home dir. The file has comments where to split the file into two parts.
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