How to run a python script taking arguments with blender? - python

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.

Related

How to run a python program from python 3.8.0 IDLE shell

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

Running a Python script from the Python Shell

I wrote a script and saved it as test.py (code shown below). And when I run it from the script (Run Module 5), I get the result in the Python Shell.
But I have tried multiple suggestions available online to have it run from the Python Shell instead to which I fail (one error pasted below).
How can I run a python script from the python shell?
The version of Python I am running is 3.7.3 and in Windows.
#!/usr/bin/python3
print(" Hello, world!")
exec(open(test.py).read())
Output:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
exec(open(test.py).read())
NameError: name 'test' is not defined
You don't need the last line for it to run. All you need is:
!/usr/bin/python3
print(" Hello, world!")
If you want to run it from another file, don't use exec. Instead, import it.
import test
You need to pass the "test.py" as a string (use quotes).
test is not a known object.

NameError when running .py file from CMD Windows 10

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

Calling python script from a Bash script

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

system commant wont work as in shell when calling a python comman on Ubuntu from R

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/bi‌​n:/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/bi‌​n:/sbin:/bin:/usr/games")
Note that system2 has a different calling convention than system.

Categories

Resources