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.
Related
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.
I tried to install VIM-Latex using the Pathogen plugin on my machine which is running OSX Lion 10.7.5. I copied the downloaded VIM-Latex plugin files into my ~/.vim/bundle directory.
I also edited the .vimrc according to the instructions specified here.
However, I'm getting the following errors on trying to open a tex document in MAC Vim:
File "/Users/username/.vim/bundle/vim-latex-1.8.23-20130116.788-git2ef9956/ftplugin/latex- suite/outline.py", line 12, in <module>
import StringIO
ImportError: No module named StringIO
Error detected while processing /Users/username/.vim/bundle/vim-latex-1.8.23-20130116.788-git2ef9956/ftplugin/latex-suite/main.vim:
and
File "/Users/username/.vim/bundle/vim-latex-1.8.23-20130116.788-git2ef9956/ftplugin/latex-suite/pytools.py", line 1, in <module>
import string, vim, re, os, glob
ImportError: No module named string
Also, I ran a basic python script from the terminal that imported StringIO and string, and both seem to be getting imported just fine.
I'm not sure where the problem is here. Since I'm really new both with VIM and Installing Plugins, I'm not sure how I should go about debugging this issue, and so, any help will be precious!
Thanks!
StringIO and string are not available in Python 3.x. To make this code work, you have to run it with Python 2.x, e.g. Python 2.7.
We have multiple Python versions installed into a remote location. /remote/Python-2.7/bin/python or /remote/Python-2.7.2-shared/linux32/bin/python etc...... In the code, we use /remote/Python-2.7-shared. I need to use module which is installed in Python-2.7 (like numpy,matplotlib) but not shared location.
In the code we begin Python code like
#! /usr/bin/env py
is it possible to import module from different Python version.?
One suggestion, I got from Google search. We can change the python path at first line of your code.
#! /remote/Python-2.7/bin py
But it also does not have some package which installed in shared and required into code.
Could I have input to fix this issue.
I could not understand what is the reason for IT guys to installed multiple version of Python . I can raise ticket also which require lot of approval installing same package in shared location( or In short, no ticket for installing package)
Note. I have tried all option but seems nothing is working. Maybe I am doing mistake.
How to import a module given the full path?
Any input will help me lot.
I tried below suggestion But end up with following error. sys.path.inser(0,"path_location")
Traceback (most recent call last):
import numpy
File "/remote/Python-2.7.2/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/remote/Python-2.7.2/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/remote/Python-2.7.2/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/remote/Python-2.7.2/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/remote/Python-2.7.2/lib/python2.7/site-packages/numpy/core/__init__.py", line 5, in <module>
import multiarray
ImportError: /remote/Python-2.7.2/lib/python2.7/site-packages/numpy/core/multiarray.so: cannot open shared object file: No such file or directory
The "shebang" line is an opportunity for the script to tell *nix-ish OSes which script executable to use for running the script. If you wish to specify a particular installed version of the Python interpreter, this would be a good way to do it in your situation.
If you wish to import modules outside of your default path/PYTHONPATH, you can two options:
Modify your PYTHONPATH environment variable
in your script, import sys and modify your path like so:
import sys
sys.path.insert(0, "path_to_module")
This customizes script's path for the duration of the script and will allow any following imports to find the target file.
virtualenvs are the preferred option. But sometimes its about just getting the task accomplished.
It looks like you really need are Virtual Envs (Virtual Environments). They are the de facto way to have multiple Pythons installed in the same machine.
I recommend you to read this Wikipedia article about SheBang. It will help you to understand what is happening.
Your first line (the SheBang) should be the executable of the interpreter that you want to execute. Use the one that has the library you want installed. Don't let any spaces between the exclamation point and the command.
Try:
#!/remote/Python-2.7.2-shared/linux32/bin/python
or
#!/remote/Python-2.7/bin/python
You should be able to run in a shell the interpreter you want to use, and then import the module you want to use:
$ /remote/Python-2.7/bin/python
Python 2.7.3 (default, Dec 18 2012, 13:50:09)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
If you can't run these commands in the console of your remote machine, you should change the interpreter you are using. Don't use #!/bin/env python, since it will use your user environment path to decide which python to use. You'll have more trouble to discover what is happening. Use directly the interpreter you want to run.
To keep it simple, don't use the #!
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.
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.