I'm afraid this will a question for a very particular case. At university we have been told to generate documentation by using pydoc. The problem is that we need to create it for a Maya script, and pydoc yells when it finds import maya.cmds as cmds
So, I tried to comment this line but I keep getting errors:
python C:\Python27\Lib\pydoc.py Script.py
problem in Script - <type 'exceptions.NameError'>: global name 'cmds' is not defined
I also tried Script, without the extension .py but it's silly doing that, we still running around the same issue.
Does anybody know how to generate documentation for Maya scripts, where import maya only works in the maya interpreter?
maya.commands is an stub module until it's run inside a working maya environment; if you just import it and inspect it outside of Maya you'll see that it's basically a placeholder.
If you want to inspect the contents, you can import the maya.standalone module and initialize it before running other commands (in this case it means you won't be able to run pydoc standalone.
You can get the documentation using the write command:
import maya.standalone
maya.standalone.initialize()
import pydoc
import mymodule
pydoc.write(mymodule) # writes the mymodule.html to current directory
Be warned, however, that the documentation for all maya built in functions will be unhelful:
'built-in function ls'
however you can at least document your own stuff without the maya parts crashing.
Pydoc, ironically, does not have a lot of external documentation. However you can see the code here:http://hg.python.org/cpython/file/2.7/Lib/pydoc.py (i'm not sure about the delta between this and the 2.6 version for maya pre-2014 but it works as above in maya 2011)
Related
Using pycharm with python 3.7. I am using queue.SimpleQueue. The code runs fine, and PyCharm is pointed at the correct interpreter and all that. But with this code:
import queue
Q = queue.SimpleQueue()
I get a warning "Cannot find reference 'SimpleQueue' in 'queue.pyi'".
I do some searching. I hit ctrl-B on the "import queue" statement and it takes me to a file called queue.pyi in the folder helpers/typeshed/stdlib/3/ under the pycharm installation. So apparently instead of the queue.py file in lib/python3.7/ under the python venv, it thinks I'm trying to import this queue.pyi file instead, which I didn't even know existed.
Like I said, the code runs fine, and I can simply add # noinspection PyUnresolvedReferences and the warning goes away, but then the type inferencing and code hints on the variable Q don't work.
Another fix is to instead import _queue and use _queue.SimpleQueue, because apparently in python 3.7 queue.SimpleQueue is implemented in cython and is imported from a cython package _queue. But importing _queue seems hackish and implementation-dependent.
Is there a way to tell PyCharm that import queue means the actual lib/python3.7/queue.py as opposed to whatever helpers/typeshed/stdlib/3/queue.pyi is?
It was fixed in PyCharm 2019.3 https://youtrack.jetbrains.com/issue/PY-31437, could you please try to update?
I have a basic question about how to package a python script with a local copy of a module, in this case the yaml module, so that i can run it on another machine and not have to download/install the module.
I tried taking the 'yaml' directory from the repo on github, putting it in the root of my python script, and simply running an import statement, but that doesn't seem to work; when i run import yaml or from yaml import load, my script fails with exception unexpected token '='.
I'm sure that I'm missing something obvious, but I wasn't able to find a clear answer by looking around for a bit. Any help would be appreciated.
I'm working through Learn Python the Hard way, and I'm currently on exercise 46 where you learn to create packages. I've created a basic package that does a few calculations, and uses a few different modules.
I've gotten the package to install in my python2.7 site packages, but I can't seem to run the module from my site packages after the fact. I'm wondering if the path that python is searching is different, because of the following:
After the install, I see this message Copying story-0.1-py2.7.egg to /usr/local/lib/python2.7/site-packages
However, when I try to run the module, I see this message /usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'story.py': [Errno 2] No such file or directory
Sorry if this makes absolutely no sense, I'm very new to the fantastical world of programing.
When you install a package, you access the internals differently. You can no longer just call python story.py.
To access the functions in story.py you need to import the module at the top of another python file, or in the interpreter.
If story.py contained
def my_test_function(blah):
print blah
You would use this function in another file by the following (after installing the module, as you've already done)
import story
story.my_test_function("Hello!")
By importing the module, you get access to all the functions and classes inside it by typing module_name.function_name. You could also just import the function you wanted to use directly, which would look something like
from story import my_test_function
my_test_function("Hello!")
I'm trying to use a Logo Language Compiler that uses Ply into the Unity3D environment for an Open Source project https://github.com/ssouzawallace/blocks-programming.
To do so I am using IronPython that is a Python interpreter running in .NET (I need this to run in Uinty3D). There is a bug in IronPython and i found others with the same issue related to the traceback of python script execution.
In resume if I run the Logo Compiler using the official Python interpreter everything goes OK. But in IronPython, when the code pass trough the get_caller_module_dic method it cannot find my pyLex stuff because it cannot reach the second frame level.
In order to resolve the problem I am wondering to pass the proper object or module to the method:
def lex(module=None,object=None,debug=0,optimize=0,lextab="lextab",reflags=0,nowarn=0,outputdir="", debuglog=None, errorlog=None):
But I don't know how to do this.
Someone know what can I do?
Thank you very much in advance
Found solution here Python: How do I get a reference to a module inside the module itself?
Now I pass the entire script as a module instead hoping the lex and yacc script find my module using the traceback.
Using
import sys
current_module = sys.modules[__name__]
This is the first time I have attempted to use anything other than what's provided by python.
I have recently gotten into pythons provided Tkinter, though due to some issues I decided to use another GUI, and heard that PyQt was highly recommended, so I downloaded that and looked into various tutorials. In these tutorials, I cannot seem to execute any of the import statements in said tutorials that relate to PyQt, primarily PyQt5 (I have checked I have the correct version number by the way).
So for instance:
import PyQt5
raises the error:
Traceback (most recent call last):
File "/Users/MEBO/PycharmProjects/Music/testing.py", line 1, in <module>
import Qt
ImportError: No module named 'Qt'
[Finished in 0.1s with exit code 1]
I have a lot of research into this. I've heard people talk of using pip to install modules, and I have done this be safe (as well as downloading it from the internet), I've tried changing the project interpreter to versions Python3/ 2.7/ 2.6, appending the path name to the sys.path directory, (which I really know nothing about to be honest, I was hoping I'd get lucky), though nothing seems to work.
Are you supposed to be able to just import a module off the bat, or do you have to set some things up first?
For windows download the package and extract it to (path where python installed)\Python27\Lib and then try to import.
Specific to PyQt
This package cannot just be downloaded and imported, it must be built because it is not pure python, it uses Qt (C++) and requires dependancies. Read this tutorial on installation.
There is also a very complete python package distribution, Anaconda, that includes pyqt and much more. Almost all the packages I ever looked at are in there.
In general to pure python code
In other cases, if you place modules/code that has been download into the directory that your python script is run from, you can import off the bat, or you can append/insert any folder to the sys.path.
# importer will search here last
sys.path.append('/path/to/code/')
# importer will search here second, right after script's directory
# this can be useful to override a module temporarily...
sys.path.insert(1,'/path/to/code/')