I have developed an AppleScript which needs to call a python file. i.e autorun.py the Autorun.py start with
import msoffcrypto
import pathlib
import os
....
Both the AppleScript and the python file run fine. I even tried to call autorun.py in the terminal and that also runs with no problem. But when the Applescript tried to call the python file:
set myPythonScript to POSIX path of "/Users/zhouyu/Library/Application Scripts/com.apple.mail/autounlock.py"
set myVal to do shell script "python" & space & myPythonScript's quoted form
display dialog myVal
It failed at the first line in the python code when Applescript tried to call it.
error "Traceback (most recent call last):
File "/Users/zhouyu/Library/Application Scripts/com.apple.mail/autounlock.py", line 2, in
import msoffcrypto
ImportError: No module named msoffcrypto" number 1
Unlike Terminal.app, do shell script does not read your shell profile so make sure you give it the full path to your python interpreter, e.g.:
do shell script "/usr/local/bin/python3" & space & myPythonScript's quoted form
Related
I am keeping a folder inside my windows machine(my_codes) where I keep all my codes. I have added this folder path to my path env variable inside windows path.
Now when I call the script from somewhere I am not able to pass the argument correctly.
(base) C:\Users\abc>test.py abc.csv
print_test
C:\Users\abc\my_codes\test.py
Traceback (most recent call last):
File "C:\Users\abc\my_codes\test.py", line 4, in <module>
print(sys.argv[1])
IndexError: list index out of range
The python code is as below. I have saved it as test.py inside C:\Users\abc\my_codes
import sys
print('print_test')
print(sys.argv[0])
print(sys.argv[1])
This works perfectly inside linux but unable to get it work in windows so far.
This works inside windows too if I call it this way.
(base) C:\Users\abc>python my_codes/test.py abc.csv
print_test
my_codes/test.py
abc.csv
However I do not want to call by specifying the full path each time to the python file.
I created a function in powershell as below:-
function test {
python C:\Users\abc\my_codes\test.py $args
}
(base) C:\Users\abc>test abc.csv
print_test
my_codes/test.py
abc.csv
and I am able to call test abc.csv. It serves my purpose but I would have loved to get it done in more proper way.
I want to launch the program Tip_and_Tax_Calculator.py from a folder named Python27 in a python 3.8.0 IDLE shell but I want to know which code will work. I have tried the exec function and execFile but they do not work, I don't know if I am using them in the wrong way too.
import Tip_and_Tax_Calculator
exec('C:\Python27\Tip_and_Tax_Calculator.py')
The error I get is:
Traceback (most recent call last): File "C:/Users/Jayan
Subramanian/AppData/Local/Programs/Python/Python38-32/Project
Launcher.py", line 1, in import Tip_and_Tax_Calculator
ModuleNotFoundError: No module named 'Tip_and_Tax_Calculator
you could use the exec function combined with open and read in the following way for python 3.x:
Notice I change from \ to /:
exec(open('C:/Python27/Tip_and_Tax_Calculator.py').read())
The method you tried and specified is relevant for python 2.x.
Another issue, you do not need to import the Tip_and_Tax_Calculator
EDIT
after the user added the error message the problem that the script didn't run is the fact the module Tip_and_Tax_Calculator is not in the current shell directory, so one need to specify its full/relative path
I am trying to run a .py file created in a text editor from the CMD line in Windows 10. Here is my very simple code:
def main():
print 'It works!'
if __name__ == '__main__':
main()
When I run from CMD line, which is already in python 2.7 mode, i type
pytest.py
which is the name of the file. However, now the CMD line says:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'pytest' is not defined
You cannot run the .py file from the Python interpreter (starting with >>>)
So, you need to see C:\Users\Eric> python pytest.py to run python on your file.
Or, you can run only python, then you must import the file.
>>> import pytest
>>> pytest.main()
Both cases assume the CMD is at the same directory as your file. If not, you must cd to that proper directory first, or use
C:\Users\Eric> python C:\Users\Eric\full\path\to\pytest.py
When you start a terminal in windows via CMD, you are in the Windows Command Line.
Here you can run your python code by entering
python yourpythoncode.py
Or you can choose to start the python interpreter by entering just :
python
In the interpreter you can run your python program by importing it
import yourpythoncode
If yourpythoncode has a line like
if ___name___ = ___main___:
main()
then it is protected from autorunning the code.
So to run your code your still need to call it explicit by entering :
main()
Either make the file executable or supply it to python program to run it
python pytest.py
If you are running the file from within the python interpreter, then you need to exit that using Ctrl + Z and run it from the command line the way I mentioned above.
Note: You will need to change to the directory where pytest.py is located in for the above command to work; or you need to supply the path to the file. For example, from your pictures, you are in the root directory i.e. C:\Users\Eric; if you open file explorer on windows and navigate to where your file is located, you can right click the file and view properties and this should show you the location. Then in your command prompt, you need to type cd C:\location\you\just\copied\ then after that you should be able to run the file using the python command above
I'm trying to call a python script from a bash script. I get import errors only if I try to run the .py from the bash script. If I run with python myscript.py everything is fine. This is my bash script:
while true; do
python script.py
echo "Restarting...";
sleep 3;
done
The error I get:
Traceback (most recent call last):
File "script.py", line 39, in <module>
from pokemongo_bot import logger
File "/Users/Paolo/Downloads/folder/t/__init__.py", line 4, in <module>
import googlemaps
ImportError: No module named googlemaps
There is more to this story that isn't in your question.
Your PYTHONPATH variable is getting confused somewhere along the way.
Insert a couple quick test lines:
in bash:
echo $PYTHONPATH
in your python:
import os
print os.environ["PYTHONPATH"]
At some point, the path to googlemaps got lost.
Your problem is in the script itself, your bash code is OK!. If you don't have problem running python scrip.py from bash directly, you should test if you use the same interpreter for both calls. You can check the shebang line in the python script, it is the first line in the file for example #!/usr/bin/env python or #!/usr/bin/python and compare it to the output of which python command if the output is different try to change or add the shebang line in to the file. If you call directly file in bash ./some_script.py bash reads the first line and if it is shebang line he wil execute the specific command for the file. My point is that if you use two diferent interpreters for calling file directly with python script.py and indirectly ./script.py one of them may not have the proper python modules.
Howto code:
$ which python
/usr/local/bin/python
So the second line is the path for your interpreter to build a shebang from it write in the first line of your script file this.
#!/usr/local/bin/python
I am trying to run a script using an interface created with tkinter. I have a button that executes a script which code is:
subprocess.call("python3 " + PATH_TO_SCRIPTS + "main.py 1 &", shell=True)
However, when this button is pressed I am getting the following error.
Traceback (most recent call last):
File "/home/m//PycharmProjects/ROSAutonomousFlight/catkin_ws/src/ardrone_numeric_method_controller/scripts/main.py", line 17, in <module>
from controller import *
File "/home/m/PycharmProjects/ROSAutonomousFlight/catkin_ws/src/ardrone_numeric_method_controller/scripts/controller.py", line 5, in <module>
import rospy
It says that the module rospy does not exist, but when I run
import rospy
using python or python3 it is imported successfully. What can I do to solve this issue? I am using Ubuntu.
The comments to your question are mostly about Python, but I guess it is more of a ROS issue.
You don't have to set-up your PYTHONPATH manually to find rospy but you have to source the setup.bash of your catkin workspace (otherwise none of the ROS tools is found).
Usually this is done by adding something like
source ~/catkin_ws/devel/setup.bash
to .bashrc. This works fine for everything that is run in a terminal.
I don't know how you start your script but as it provides a graphical interface you probably just run it by double-clicking it in the file browser? If you indeed do so, the script is not run in a terminal and therefore can't find the ROS modules. Run the script from a terminal (in which the setup.bash has been sourced) and it should work.