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 #!
Related
I am using python/selenium in visual studio code. I am trying to import my another python class driverScript which resides in executionEngine module and in the file DriverScript. I have imported as below:
import driverScript from executionEngine.DriverScript
It is producing error:
Traceback (most recent call last):
File "c:/Selenium/Selenium-Python Framework/RK_Practice/Tests/mainTest.py", line 5, in <module>
from executionEngine.DriverScript import driverScript
ModuleNotFoundError: No module named 'executionEngine'
How can I import correctly? Your help is very much appreciated.
If the script you are wishing to import is not in the current directory, you may want to take this approach:
import sys
sys.path.insert(1, '/path/to/script/folder')
import driverScript from executionEngine.DriverScript
If your python file is on the same level in dir, then you can import just by calling:
import filename
If your python file is inside another folder, then you need to create a blank __init__.py file that will help python to understand it's a package. Then you can import that as follows:
from folderName import filename
Depends on where executionEngine is supposed to come from. If it's from a package installable via a package manager such as pip or Anaconda, looks like it's not properly installed. If you installed it yourself, you probably need to add the directory containing executionEngine to your PYTHONPATH, so the Python interpreter can find it. This can be done in the VSCode environment files. See the PYTHONPATH section in https://code.visualstudio.com/docs/python/environments
The first two lines of the program are:
from i2clibraries import i2c_lcd
from ABElectronics_ADCPi import ADCPi
No matter what line is first the Pi returns an error when I attempt to run it under Python or Python 3. All the libraries are possessed and registered. Using the shell commands the checks saying the exports worked correctly all show up correctly. However, whatever line is line 1 will return a missing module error and the i2clibraries will always return a missing module error. By keeping that as the first line I get the least errors in running, but the program still returns this:
pi#raspberrypi ~ $ sudo python file.py
Traceback (most recent call last):
File "file.py", line 1, in <module>
from i2clibraries import i2c_lcd
File "/home/pi/i2clibraries/i2c_lcd.py", line 1, in <module>
from i2clibraries import i2c
File "/home/pi/i2clibraries/i2c.py", line 1, in <module>
from quick2wire.i2c import I2CMaster, writing_bytes, reading
ImportError: No module named quick2wire.i2c
Given the error, what possible solutions are there to stop the first line from being unable to find its module?
Problem
The error message is telling you that when you try to import the i2clibraries module, the imports that it requires (dependencies) cannot be found when it is itself is being imported. This is specifically in the first line of i2c.py file - where the line
from quick2wire.i2c import I2CMaster, writing_bytes, reading
is failing.
The problem is almost certainly that your modules are not on the Python module search path. Further info on this is given at the end of this answer should you need it.
Solution
There are a number of ways to resolve this. The one recommended by the developers of the module is
To use the library without installation, add the full path of the
source tree to the PYTHONPATH environment variable. For example:
export QUICK2WIRE_API_HOME=[the directory cloned from Git or unpacked from the source archive]
export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME
So, you need to know where your quick2wire libraries are installed - from your error message, I would hazard a guess that they are in /home/pi/i2clibraries/, so $QUICK2WIRE_API_HOME=/home/pi/i2clibraries/ should be your first line of the above pair.
Further info
You can read more generally about how to install modules on Python 2.x on the Python website. You can look at what paths make up the module search path by going to an interactive Python prompt (i.e. typing python) and then doing.
>>> import sys
>>> sys.path
This will output a list containing strings representing all the paths that will be searched for modules.
Somehow my python is broken and emits the error:
jseidel#EDP15:/etc/default$ python -c 'import random'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.6/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
This is NOT the virtualenv error that is so commonly documented here and elsewhere: I don't use python directly, I have never setup a virtualenv explicitly, and there is no virtualenv directory or python script that I can find anywhere on my system.
I'm running Kubuntu 10.04 and until just recently my KPackageKit worked just fine and handled updates with no problem. Now it shows nothing... maybe because of this python error, maybe because of something else.
How do I go about finding the error and fixing python?
As suggested by #Armin Rigo, this worked for me:
1) Add a print 42 at the end of the /usr/lib/python2.6/os.py file.
2) If you see "42", then that is the correct os.py file and the urandom module is not include. Add the statement to include urandom (you can find a sample from another os.py file). This was what worked for me.
3) If you don't see "42", then that's not the os.py file that you're using. Find the random.py file that is crashing and insert import os; print os.__file__ to get more information about the failure.
I can't seem to import the email module at all. Every time I do it I get an error. I've tried uninstalling Python and reinstalling, but the email module just refuses to work. I've even done "pip install email" and it's still broken. I'm on Windows 7 Home Premium x64, running an x86 version of Python.
Here's what happens:
c:\Users\Nicholas\Desktop>python
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import email
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "email.py", line 1, in <module>
import smtplib
File "C:\Python27\lib\smtplib.py", line 46, in <module>
import email.utils
ImportError: No module named utils
>>>
EDIT: I've tried both Python from python.org and ActivePython, thinking ActivePython might work. Is there anyway to completely remove python and all its data and start 100% fresh maybe?
It looks like you have a file named email.py. Don't use file names that have the same name as Python standard library modules. Generally, your working directory comes earlier on the Python search path for importing modules so files in your working directory will override modules with the same name in the standard library.
The clue: note the path names in the traceback
File "email.py", line 1, in <module>
import smtplib
File "C:\Python27\lib\smtplib.py", line 46, in <module>
import email.utils
By the way, this is a very common error. The excellent tutorial in the Python standard documentation set talks about it here.
I just came across this error and wanted to share my solution. In my case, I had a file named email.py in directory. This created a name conflict between Python's email.py and my file. When smtplib tried to import email.utils it looked and my file and didn't find anything. After I renamed my copy of email.py into myemail.py everything worked like a charm.
I also came across this error. In addition to renaming the email.py to something else, you must also remove the email.pyc (notice the C) file. After that, all is well. Thanks all!
I also fetched this problem because i had a file named email.py in my project directory. I wasn't able to import urllib.request . When i changed the file name email.py to emailtest.py then the error gone away.
In every time we should not use the name what is same as python core file name.
If none of the posted solution works, try the following:
(Use the combo python3 / pip3 OR python / pip and try not to mix those)
Delete your current virtual environment directory.
Create new virtual environment and activate it with 'source'.
If exist, run the <pip install -r requirements.txt>.
Install any missing packages with pip or pip3.
Run the application to see if that solved the problem.
npm install email
has fix my problem, try it.
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.