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.
Related
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
I have this Python code: https://github.com/andreagrandi/aoc_2019/tree/master/aoc
which runs perfectly from the terminal (Python 3 required) if I do for example: python aoc_03.py
But if I try to run it from VSCode, taking advantage of the Python extension and integration, I get this error:
(aoc) ➜ advent_of_code_2019 git:(master) /Users/andrea/.virtualenvs/aoc/bin/python /Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py
Traceback (most recent call last):
File "/Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py", line 70, in <module>
with open('aoc_03_input.txt', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'aoc_03_input.txt'
My guess is that when invoked from VSCode, the script is run from a different path, so it cannot find the file aoc_03_input.txt which is located in the same folder of the script.
How do I tell VSCode to run my script from the /Users/andrea/Documents/advent_of_code_2019/aoc/ folder, so that it will be able to find my input file?
Thanks
Actually, I should have tried more before asking this question, because I just found the solution, but at this point I will write it here in case it can be useful to anyone:
If I change the path in this way:
with open('./aoc_03_input.txt', 'r') as file:
The file is being open correctly when I run the code in VSCode and when I run it from the terminal. Tested under OSX (but it should work under Linux too. Not sure about Windows).
I have a script I want to run within blender to generate AO maps (script was given to me and the source guarantees it works).
I try to run the script as follows:
blender --background --python /opt/ff/product_builder/furniture_builder/generate_ao_maps.py --input_dir /tmp/test.obj --output_dir /tmp/test.png --mode ao
Which produces:
AL lib: (EE) UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead
found bundled python: /usr/share/blender/2.79/python
Traceback (most recent call last):
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 195, in <module>
main()
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 178, in main
args = parse_args()
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 21, in parse_args
return parser.parse_args(os.getenv(BLENDER_ENV).split(' '))
AttributeError: 'NoneType' object has no attribute 'split'
Error: File format is not supported in file '/tmp/test.obj'
Blender quit
If I run this same script without blender (but with the argument) it tells me:
Traceback (most recent call last):
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 5, in <module>
import bpy
ImportError: No module named bpy
What do I need to do to pass the parameters to the script and get it working?
You are seeing that error because your script is looking for the environment variable BLENDER_ENV, which is not on your system. I don't recognize BLENDER_ENV as a standard Blender related environment variable so it's probable that your friend added BLENDER_ENV to his or her environment.
Firstly, blender processes its cli args in the order they are given, so your example will start in the background, run a script, then set the input_dir... This will most likely not have the result you are after.
The problem with your script failing is that the arg passed to os.getenv() needs to be a string that is the name of a shell environment variable, if you are using bash you need to export the variable to put it into the environment before you start blender.
export BLENDER_ARGS="arg1 arg2"
blender -b myfile.blend --python myscript.py
If you are using a csh then use setenv BLENDER_ARGS "arg1 arg2"
Then in your py script, you use os.getenv('BLENDER_ARGS').split(' ')
Note that each shell instance is a separate environment, you need to set the variables in the same instance that starts blender.
You may also be interested in passing cli arguments to the script as explained in response to this question.
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 invoke within R a system command that invokes a call to a python script (that includes an import to pandas) as following:
getwd()
[1] "/home/production"
> system("python In_tag_main_model/python_scripts/connect_to_couchbase.py")
Traceback (most recent call last):
File "In_tag_main_model/python_scripts/connect_to_couchbase.py", line 11, in <module>
import pandas as pd
ImportError: No module named pandas
Within connect_to_couchbase.py i'm calling pandas, which isn't recognized, though when i'm running this exact command from my machines shell:
production#va-rsrv01:~$ python In_tag_main_model/python_scripts/connect_to_couchbase.py
production#va-rsrv01:~$
It works great,any ides why system isn't working for me?
Thanks in advance!
It looks like the R system function is executing a different python executable. You have three options to specify which executable you want:
You the absolute path:
system("/anaconda2/bin/python In_tag_main_model/python_scripts/connect_to_couchbase.py")
Set the PATH variable for your process via Sys.setenv (as you have done):
Sys.setenv(PATH="/anaconda2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games")
system("python In_tag_main_model/python_scripts/connect_to_couchbase.py")
Use the newer system2 function which provides an env argument that can be used to modify the environmental variables for the subprocess:
system2("python",
args="In_tag_main_model/python_scripts/connect_to_couchbase.py",
env="PATH=/anaconda2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games")
Note that system2 has a different calling convention than system.