Running mayapy on mac - python

I am trying to run a python script using maya's python interpreter. I am writing this script to be placed in a pipeline, so that maya runs in batchmode. Nothing is happening when I run this command:
maya -batch -script maya.py $1
I get the following message after running this command:
I also tried using the python interpreter directly with a test file
/Applications/Autodesk/maya2017/Maya.app/Contents/bin/mayapy test.py
test.py looks like this
`import maya.standalone
try:
maya.standalone.initialize()
except:
print "standalone already running"`
I get this error ImportError: No module named cmds
I have looked at this post, but it did not help me. What am I doing wrong?

Related

Execute commands from python prompt using os.system

I am trying to execute a few commands using python shell to import a module ABC. For that I have created a python file test.py having the following lines:
import os
os.system('python')
os.system('import ABC')
Now I am trying to execute that file as follows - python test.py. Here, it runs till the 2nd line i.e. os.system('python') properly. Then when it enters into the python prompt and there it remains stuck as it is not command prompt any more but python prompt. So how to execute the next command of importing the module ABC on the python prompt?
Command line and environment (Python Documentation)
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
In your terminal, when you run python followed by the name of a script (test.py), you are executing the contents of the script. Within that script, when you call 'python' using os.system('python'), you are starting a new interactive session, similar to if you were to just call 'python' from your terminal.
import os
os.system('python -c "import ABC"')
You can use the -c argument with python.
For example,
import os
os.system('python -c "import ABC"')
It sounds like what you need is to put the commands you require in a Python script, e.g. my_imports.py:
import ABC
import my_module
And run it from interactive Python with something like:
exec(open('my_imports.py').read())
That will execute the code in the script in the current context, making the imports available to you on the Python CLI.

Anaconda prompt: ModuleNotFoundError when running Python script

I am trying to run the following script "test.py" on Anaconda Prompt:
from tensorflow.keras.applications.resnet50 import ResNet50
...with the following command:
(conda_env) C:\dev>test.py
This results in the following error:
ModulNotFoundError: No module named 'tensorflow'
When I run the same script on Anaconda Prompt with the following command I don't get any errors:
(conda_env) C:\dev>python test.py
I have installed tensorflow in the Anaconda environment 'conda_env'
(conda_env) C:\dev\>conda env list
# conda environments:
#
base C:\Users\xx\Anaconda3
conda_env * C:\Users\xx\Anaconda3\envs\conda_env
keras_1 C:\Users\xx\Anaconda3\envs\keras_1
tf-gpu C:\Users\xx\Anaconda3\envs\tf-gpu
Why is this like that?
You won’t get errors if you do
(conda_env) C:\dev> python test.py
because then you’re following the correct syntax for running python scripts in a terminal. By adding python before the .py file, you initiate the Python interpreter that executes your script. Without it, the terminal won’t know what Python interpreter to use to execute your script and may end up using an interpreter that doesn't have the modules you require. There are ways to skip writing python before executing if that’s what you want.
For example, see: Calling a python script from command line without typing "python" first

python gives ImportError: No module named "" when triggered in a perl program

In my perl program which runs the python script
I have provided the PYTHONPATH env param with the path for the lib and i have run the python script. I am getting
ImportError: No module named "....."
my $script = "/path/pythonscript.py";
$ENV{'PYTHONPATH'} = "/path/lib";
system("python $script");
Whereas when i run the same python script on command line in the same directory where the script executes in my perl program, it is working.
Can anyone give me some pointers on why this is happening.
Try printing the contents of sys.path and compare the difference e.g. change your python script to
import sys
print(sys.path)
Most likely there is a difference here and this is causing the module to not be found.
I once had a similar problem. I solved it by creating an executable script (chmod) and making that script run instead of the python script. The script simply contained a cd to the directory and a python3 program. py

Python Tornado AttributeError: module 'test' has no attribute '__path__'

I am attempting to just run the Hello World code from Tornado docs
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Except I am getting an error: AttributeError: module 'test' has no attribute '__path__'
I am just using IDLE to run test.py
I thought this was due to my Windows 10 computer not having Python accessible to PATH but even with adding in the python 3.6 to PATH I am still getting the same error. Any ideas?
The screenshot is how I added python to PATH and I think I got it correct..
------EDIT------
Ill add some screenshots of the errors/tracebacks I am running into. 1st one is the command prompt below when the test.py is ran in IDLE 3.6 in Windows 10.
If there is an import error, I can import Tornado just fine thru IDLE interpreter.
I also tried running this hello World code in IPython 3.7, and I get this error:
Solution: Run your file without the -m argument.
Another solution would be to provide the file name without the .py extension:
python -m test
This will also work.
Explanation:
The -m argument tells Python to run a module (file) present in the Python path. It doesn't take the name of the file, it takes the name of the module. The difference is that the file name contains .py suffix, whereas the module name doesn't.
So you can run the test.py file like this, too: python -m test.
When to use -m argument:
The -m argument is there for convenience. For example, if you want to run python's default http server (which comes with python), you'd write this command:
python -m http.server
This will start the http server for you. The convenience that -m argument gives you is that you can write this command from anywhere in your system and python will automatically look for the package called http in your the system's Path.
Without the -m argument, if you wanted to run the http server, you'd have to give it's full path like:
python C:\path\to\python\installation\http\server.py
So, -m argument makes it easy to run modules (files) present in the Path.
With Tornado would you happen to know how to kill the Python interpreter? A CNTRL-C doesn't do anything.
I use Linux and Ctrl-C works fine for me. On Windows you can try Ctrl-D or Ctrl-Z. Or here are some answers: Stopping python using ctrl+c

Run .py script via sh import module error

This is a very basic question on how to code in python and run your script from a very beginner.
I'm writing a script using Xcode9.4.1 which is supposed to be for python3.6. I then have an sh script run.sh, in the same folder of the script (say "my_folder") which simply looks like
python my_script.py
The python script looks like
from tick.base import TimeFunction
import numpy as np
import matplotlib.pyplot as plt
v = np.arange(0., 10., 1.)
f_v = v + 1
u = TimeFunction((v, f_v))
plt.plot(v, u.value(v))
print('donne!\n')
But as I try to run my_script.sh from the terminal I get a "ImportError: No module named tick.base" error.
But the tick folder is actually present in "my_computer/anaconda3/lib/python3.6/site-packages" and up to last week I was using Spyder from anaconda navigator and everything was correctly working, so no "import error" occurred.
The question is quite trivial, in some sense it simply is "what's the typical procedure to code and run python script and how modules are supposed to be imported-downloaded when running on a given machine?"
I need it since my script is to be run on another machine through ssh and using my laptop to make some attempts. Up to last year I used to work in C and only need to move some folders with code and .h files.
Thank for help!
EDIT 1:
From the Spyder 3.2.7 setting, where the script was giving non problem, I printed the
import sys
print(sys.path)
The -manually- copied the content to the sys.path variable in my_script.py and rerun 'run.sh' and now getting a new (strange) error:
Traceback (most recent call last):
[...]
File "/Users/my_computer/anaconda3/lib/python3.6/site-packages/tick/array/build/array.py", line 106
def tick_double_array_to_file(_file: 'std::string', array: 'ArrayDouble const &') -> "void":
^
SyntaxError: invalid syntax
First, check the python which you are calling the script with is pointing to the anaconda python and it is of the same version you are expecting it to be. You can do "which python" command in Linux and Mac to which the path which points to python. It if is pointing to some different version or build of python than the one which you are expecting then add the needed path to the system environment PATH variable. In Linux and Mac this can be done by adding the following line in the .bashrc file at the /home/ folder:
export PATH=/your/python/path:$PATH
And then source the .bashrc file.
source .bashrc
If you are on a operating system like cent os ,breaking the default python path can break your yum so be careful before changing it.
I am running a script in PyCharm and under the Project Interpretor I have the path
C:\envs\conda\keras2\python.exe
When I try to run the script via ssh on the server I get a 'no module named' error. I get
/usr/bin/python as the ans to 'which python' on the server itself. Could you tell me which path I must add for the script to run properly?

Categories

Resources