Unable to import pexpect "windows only" methods - python

I have recently installed the pexpect 4.0 module, as it will be quite useful for the program I am creating. I do have windows, so I looked specifically at the exceptions for pexpect, knowing that the normal spawn and run methods aren't available to me. But, I cant get the "windows methods" that the module is supposed to show windows users, which are:
pexpect.popen_spawn.PopenSpawn, and pexpect.fdpexpect.fdspawn.
Does anyone have any idea how I can get these methods? I am running on windows 10, python 3.4.
Side note: I am currently working on trying to get winpexpect to import the spawn module from pexpect, but I am also failing at that as well.

This will do it for you:
from pexpect import popen_spawn, fdpexpect
Then you can do the rest of what you needed.
edit: this is the reason you did not see it, in the __init__.py notice this:
__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'spawnu', 'run','runu', 'which', 'split_command_line', '__version__', '__revision__']

Related

Import module failed even though installed

i have a couple of different questions regarding importing modules and another non-related syntax question.
So, i'm using a module called netmiko to automate networking scripts, this isn't a networking question but a python one, i create the scripts in Pycharm and then run them but when i tried to do one last night for the first time using Netmiko it's coming up with an import module failed exception which is confusing me because i've installed it using "pip install netmiko" and i saw it install AND if i do "import netmiko" using the command line on Windows then it works as well with no exceptions. So i've been building the scripts in Pycharm but having to copy/paste them into then cli which isn't great. Does anybody know what the issue may be here?
The second question is just a general syntax question. I've seen "+=" used when using the same variable names in Python (mainly used in netmiko scripts but i assume it's obviously used in other Python scripts to) such as:
output = net_connect.send_command(cmd, expect_string=r'Destination filename')
output += net_connect.send_command('\n', expect_string=r'#')
....the rest of the script isn't import, but i'm wondering what the "+=" is actually doing here because to me it seems no different than doing:
output = net_connect.send_command(cmd, expect_string=r'Destination filename')
output = net_connect.send_command('\n', expect_string=r'#')
Can anyone shed some light on this as well?
Thanks as usual everyone!!

Windows 10 notifications in python

I've tried a lot of methods found online to create notification bubbles through python, but none of them work. I suspect this has something to do with windows just not allowing them for the script, but how can I solve this?
For example, using the balloontip snippet, and in my own script:
import balloontip
Following with either of these just doesn't show any toast.
w=balloontip.WindowsBalloonTip('asdf', 'qwerty')
balloontip.balloon_tip('asdf', 'qwerty')
I've also tried using the win10toast package but still no such luck.
What's even weirder is that solutions entirely unrelated to python also don't work. For example this powershell script creates an icon in my tray but the message won't show.
Try to import all the modules used in the snippet, and include the snippet in your script. You might need to install win32api using pip install pypiwin32
This might help you identify the problem/check if it is indeed a problem with Windows notifications in general.
I was able to create a notification by adding the following code to the end:
balloon_tip("test title", "test message")
Check if you have blocked windows notifications:
http://www.howto-connect.com/disable-enable-app-notifications-on-windows-10/

Python - How do you import downloaded directories/modules?

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/')

Create documentation using pydoc and unknown modules

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)

PyDev doesn't autocomplete for some modules (numpy, matplotlib at least)

This is similar, but not this question Code-Completion for e.g. Numpy, SciPy or Matplotlib does not work in Eclipse PyDev
My problem isn't that PyDev can't resolve the variables as their specific type, I can't even get it to look into the numpy module for the functions. For example
import numpy as np
np.<ctrl+space>
gives me nothing. These modules are in my list of forced builtins.
Autocomplete does work for many other modules (including Qt/qwt, serial, struct) and the programs run fine. However, every time I use np.something eclipse thinks it is an error(red line under it). Does this "just work" for everyone else?
Edit (additional info):
There are errors in my error log. However, they are about Java not having permissions to run pylint and complaints about invalid encoding. I am running Eclipse 3.8.1 with python 2.7 on Ubuntu 13.04. Historically, I develop python in Spyder, so whatever I am missing likely has to do with Elipse nuances.
It should 'just work' for you. Can you check if 'numpy' is on the 'forced builtins' list (in your interpreter configuration -- it should be added automatically, but it's possible it's not there. See: http://pydev.org/manual_101_interpreter.html for details on it).
If it is and you still have an issue, please check if you have some error in your error log -- see: http://pydev.org/faq.html#PyDevFAQ-HowdoIReportaBUG%3F for details on getting it.

Categories

Resources