Import pexpect in child module - 'no module named pexpect' - python

I'm seeing weird behavior that I don't understand, so I'm turning to the experts here on SO for help. I have looked at similar questions, without finding anything that looked helpful.
I'm writing a program that imports two custom modules.
If I run the main program using the cli "./ld_config_automation.py" I get the following error:
laptop$ ./ld_config_automation.py
Traceback (most recent call last):
File "./ld_config_automation.py", line 34, in <module>
from modules.networkcomponent import NetworkComponent
File "/path/to/pprograms/ssh_telnet_automation/modules/networkcomponent.py", line 7, in <module>
import pexpect
ImportError: No module named pexpect
However, if I run the main program using the cli "python ld_config_automation.py" everything works fine and the program runs.
My main program uses the following import statements.
import sys
import getopt
from modules.networkcomponent import NetworkComponent
from modules.recordclass import Record
The module I'm having trouble with "NetworkComponent" uses the following import statements:
import re
import pexpect
Also, if I remove import pexpect from the NetworkComponent module it works as expected without python in front (up to when it has to use pexpect. of course). So it doesn't seem to be a problem with importing modules, as re works ok, just not pexpect.
I'm developing this on mac os X but will implement on CentOS.

Related

Regarding python error : "ImportError: cannot import name Dynamic"

I am facing a strange error while executing a python code. The following code is a small snippet of the python code I am executing:
#samplecode.py
import time
from datetime import datetime
import sys
import os
import inspect
sys.path.append(os.path.dirname('C:\Users\qksr\Desktop\work\kako\logging.py'))
import logging
from logging import Dynamic
While executing samplecode.py I am facing an error showing the following:
Traceback (most recent call last):
File "C:\Users\qksr\Desktop\work\Fire\samplecode6.py", line 8, in <module>
from logging import Dynamic
ImportError: cannot import name Dynamic
My logging.py which contains the code that needs to be imported while execution. The following is the code:
class Dynamic(object):
pfile3=open('C:\Users\qksr\Desktop\work\sample3.txt','w')
we can see that the class Dynamic is created yet the import error is thrown.
The strangest thing is I did few examples of importing files and it worked well. I have tried hard but still cannot figure it out. I would like to know why this error was thrown and why suddenly for this and not in previous samples?
Python already has a built-in logging module, which is being located before yours (you're appending your folder to the end of the path).
Rename your logging.py file to something else.

import error in python on ubuntu

I have created my first python module on ubuntu. When I'm trying to import the module in python using :
import brian
it is giving error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named brian
I have brian in /home/noamaan and python is in /usr/bin.
If you launch python from the directory that contains brian module, everything will work as it is now.
To import custom module from anywhere you want you should read attentively something on the import mechanism in python to learn where the imported modules are searched for, etc.
But to make your code work right now, I can recommend you the following:
Either extend your PYTHONPATH variable before running python, to include the directory of your module
Or append it right in the code by using sys module in this way.
import sys
sys.path.append("path/to/module/dir")
import brian
Also, see info on site module
by default Python import modules from Python path var.
You can view these paths so:
import sys
print sys.path

How/where does Python look for modules?

I am completely new to Python and wanted to use py2neo and tornado module.
In order to do that I ran setup.py for both modules and placed them into folders
C:\Python32\modules\py2neo
and
C:\Python32\modules\tornado
In the main program I guess these lines tell the interpreter where to look for files:
import sys
sys.path.append(r'C:\Python32\modules')
# Import Neo4j modules
from py2neo import neo4j, cypher
Reading the book I also added environmental variable (in Windows 7)
PYTHONPATH = C:\Python32\modules;C:\Python32\modules\tornado;C:\Python32\modules\py2neo
Edit
Now I figured out that Python Shell has to be restarted in order to load modified PYTHONPATH variable
In case the variable value is PYTHONPATH = C:\Python32\modules
and the program contains the line
from py2neo import neo4j, cypher
then the following lines are useless:
import sys
sys.path.append(r'C:\Python32\modules')
When I run the program however I get the following error:
Traceback (most recent call last):
File "C:\...\Python Projects\HelloPython\HelloPython\Hellopy2neo.py", line 15, in <module>
from py2neo import neo4j, cypher
File "C:\Python32\modules\py2neo\neo4j.py", line 38, in <module>
import rest, batch, cypher
ImportError: No module named rest
In the file neo4j.py there are the following lines:
try:
import json
except ImportError:
import simplejson as json
try:
from urllib.parse import quote
except ImportError:
from urllib import quote
try:
from . import rest, batch, cypher
except ImportError:
import rest, batch, cypher #line38
and rest.py file is located in folder C:\Python32\modules\py2neo so I don't know why I get the error
ImportError: No module named rest
Edit2:
Trying to import the py2neo directoy in Python Shell and list modules I get:
>>> import py2neo
>>> [name for name in dir(py2neo) if name[0] != '_']
['rest']
I guess there are some unneccesary imports as well and would be very thankful if anyone explained, which imports should be added and excluded (in PYTHONPATH and scripts) in order the program to run without errors.
I suspect the problem is that the import syntax for relative imports has changed in transition from Python 2 to Python 3:
The only acceptable syntax for relative imports is from .[module]
import name. All import forms not starting with . are interpreted as
absolute imports.
The modules you installed use the syntax that would work in Python 2. You could either install them for Python 2, or look for a version of py2neo that supports Python 3, or try to port it manually (the import line should look like from . import rest, but you'll probably face other problems later) or with 2to3 tool.
Update: I tried installing py2neo with pip. It failed for Python3 and finished successfully for Python 2. The version is 1.2.14.

How to import the sikuli module in python?

I'm having trouble getting sikuli+python unit testing to work, and would be glad for some assistance.
I searched the internet far and wide, but haven't anything that worked. My python knowledge is very limited, and I guess that doesn't help...
I'm using win7 32,
sikuli 1.0 rc3 (r905),
python 2.7.3
I'm trying to start running sikuli unit tests through python (not with command line, just as a python script to run with the python gui). I've followed, among others, the instructions shown here: http://sikuli.org/docx/globals.html
and here: https://answers.launchpad.net/sikuli/+faq/1804
my sikuli script (name: slidelyChromeUnitTest2.sikuli) starts like this (the rest doesn't matter becuase it gets stuck on the very first line):
from sikuli import *
import unittest
and my python script (name: SikuliTestRunner.py) looks like this:
import sys
mySikuliPath = "D:\\Program Files\\Sikuli X\\slidelyChromeUnitTest2.sikuli"
if not mySikuliPath in sys.path: sys.path.append(mySikuliPath)
import slidelyChromeUnitTest2
And what I get when I run the python script is this:
Traceback (most recent call last): File
"D:\Python27\SikuliTestRunner.py", line 6, in
import slidelyChromeUnitTest2 File "D:\Program Files\Sikuli X\slidelyChromeUnitTest2.sikuli\slidelyChromeUnitTest2.py", line 1, in
from sikuli import * ImportError: No module named sikuli
And I can't get past that error...
What am I doing wrong?
Thank you,
Ilan
It's Jython you can't.
http://doc.sikuli.org/devs/system-design.html
edit: updated the link, removed a dead post reference.

unable to import libxml2mod from the python script

File "/usr/local/lib/python2.5/site-packages/libxml2.py", line 1, in <module>
import libxml2mod
ImportError: /usr/local/lib/python2.5/site-packages/libxml2mod.so:
undefined symbol:xmlTextReaderSetup
>>> import libxml2mod
>>> import libxml2
>>>
on Python Prompt it works fine !!
can anyone has idea why my program is not working from .py file as import is working perfect from python prompt.
I can only suggest that your paths are different for some reason. Either that, or you are not using the same python interpreter in both cases.
I have experienced this when I happen to have a couple of interpreters, and the wrong one is either default, or specified in the #! section of the script.

Categories

Resources