0

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
Community
  • 1
  • 1
user765443
  • 1,856
  • 7
  • 31
  • 56

2 Answers2

3

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.

animuson
  • 53,861
  • 28
  • 137
  • 147
JS.
  • 14,781
  • 13
  • 63
  • 75
2

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 #!

neves
  • 33,186
  • 27
  • 159
  • 192
  • Thanks for reply and provide info. But it is not install in our machine. it is best solution but not feasible for us – user765443 Oct 17 '13 at 15:08
  • @AbhishekGoswami: sure, but you can install it in your user account. You don't need root permission to do it and would be able to isolate your environment and dependencies. – neves Oct 17 '13 at 18:21