Install python module to non default version of python using .sh - python

I have a problem similar to this post: Install python module to non default version of python on Mac, so I am aware of those solutions, but they do not work for me.
I am installing M2Crypto on CentOS, which means I much use fedora_setup.sh build followed by fedora_setup.sh install in order to install on my architecture.
Unfortunately, the default Python version is 2.6, but I use 2.7. How do I execute the build and install commands so that they build and install to Python2.7 site-packages? Is there a simple command I don't know? I've been searching around here: http://docs.python.org/2/install/ in the Python Docs, but I don't see anything about .sh scripts?

You should run your scripts in a virtualenv created for your app's environment. This creates an isolated environment that uses the Python interpreter you created the virtualenv with, but with its own set of libraries.
# create the virtualenv folder: M2Crypto-venv
python2.7 virtualenv.py --distribute M2Crypto-venv
# activate the virtualenv, changing environment variables to use its Python interpreter
. M2Crypto-venv/bin/activate
# see how the current python has changed
which python # should be M2Crypto-venv/bin/python
python --version # should be 2.7
# after activating, run your install scripts
If you're using mod_wsgi or something similar to serve content, you'll want to modify your WSGI file to activate the virtualenv before doing anything else (adapted from mod_wsgi instructions):
import os.path
virtualenv_path = '/path/to/M2Crypto-venv'
activate_this = os.path.join(virtualenv_path, 'bin/activate_this.py')
execfile(activate_this, dict(__file__ = activate_this))
# rest of the WSGI file...

This was an incredibly difficult answer to come by, but the support team at Webfaction where I am hosted were spectacular in assisting me. Directly from the support I was given:
First build swig,
wget http://prdownloads.sourceforge.net/swig/swig-2.0.8.tar.gz
tar -xf swig-2.0.8.tar.gz
cd swig-2.0.8
./configure --prefix=$HOME
make
make install
Than get m2crypto,
svn checkout http://svn.osafoundation.org/m2crypto/tags/0.21/ m2crypto-0.21
cd m2crypto-0.21/
Edit fedora_setup.sh from this
SWIG_FEATURES=-cpperraswarn python setup.py $*
to this,
SWIG_FEATURES=-cpperraswarn python2.7 setup.py $*
Then build, then install,
./fedora_setup.sh build
./fedora_setup.sh install --prefix=$HOME
[me#web342 lib]$ python2.7
Python 2.7.5 (default, May 16 2013, 20:16:09)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import M2Crypto
>>> print M2Crypto
<module 'M2Crypto' from '/home/me/lib/python2.7/site-packages/M2Crypto-0.21-py2.7-linux-x86_64.egg/M2Crypto/__init__.pyc'>
Obviously, substitute your own details throughout. Hope this helps the next guy trying to install M2Crytpo using fedora_setup to a non-default python version.

Related

Make pyenv build python with different sqlite version

I'm using ubuntu 22.04 and python version installed there is '3.10.4' while sqlite version is '3.37.2'. I need to use python '3.9.5' and sqlite '3.31.1' because the server that my code will live uses that.
Using pyenv fixes the problem for python but the sqlite one didn't. So I resort on building the sqlite from the source and try to link on pyenv install.
Basically this is what I tried to do:
~$ mkdir -p ~/usrapps/src
~$ cd ~/usrapps/src/
~/usrapps/src$ wget https://www.sqlite.org/2020/sqlite-autoconf-3310100.tar.gz
~/usrapps/src$ tar xvvf sqlite-autoconf-3310100.tar.gz
~/usrapps/src$ cd sqlite-autoconf-3310100/
~/usrapps/src/sqlite-autoconf-3310100$ mkdir -p ~/usrapps/sqlite3311
~/usrapps/src/sqlite-autoconf-3310100$ ./configure --prefix=/home/paulo/usrapps/sqlite3311
~/usrapps/src/sqlite-autoconf-3310100$ make
~/usrapps/src/sqlite-autoconf-3310100$ make install
~/usrapps/src/sqlite-autoconf-3310100$ cd ~/
Here's the full terminal ouput
Now for installation of python 3.9.5, I do this different scenarios but first, brew-pyenv-i is aliased to CC="$(brew --prefix gcc)/bin/gcc-12" pyenv install the reason is I encounter a issue where installing python result to "BUILD FAILED: Mising OpenSSL" even though I have openssl install. Doing this fixed my issue, here's the reference.
I uninstall first python(installed by pyenv) before each scenarios.
scenario 1 - log:
~$ LDFLAGS="-L/home/paulo/usrapps/sqlite3311/lib" CPPFLAGS="-I/home/paulo/usrapps/sqlite3311/include" brew-pyenv-i 3.9.5
scenario 2 - log:
~$ PYTHON_CONFIGURE_OPTS="LD_RUN_PATH=/home/paulo/usrapps/sqlite3311/lib LDFLAGS=-L/home/paulo/usrapps/sqlite3311/lib" brew-pyenv-i 3.9.5
scenario 3 - log:
~$ PYTHON_CONFIGURE_OPTS="LD_RUN_PATH=/home/paulo/usrapps/sqlite3311/lib LDFLAGS=-L/home/paulo/usrapps/sqlite3311/lib CPPFLAGS=-I/home/paulo/usrapps/sqlite3311/include" brew-pyenv-i 3.9.5
All attempts are successfully installed the python 3.9.5, but when I try check the sqlite version, its still same, not '3.31.1'
~$ pyenv shell 3.9.5
~$ python
Python 3.9.5 (default, Aug 26 2022, 08:45:14)
[GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.37.2'
I would like to avoid using pysqlite3, because that's mean I have to rewrite some of my code and my goal is not really to use multiple versions of sqlite in my app.

Need Help Downloading Albow Library

I am trying to download a library called Albow. I tried using pip to install it, but it didn't work, so I went to the Albow website and it linked to a .zip file. Basically what I want to do is to make it so that when I type:
import albow
Python recognizes it.
I'm using Python 3.5.3, on a Debian Linux VM. My PC's OS is using ChromeOS.
If I missed something that I should explain, I will edit the question.
The Albow website you linked to (or this other one from the Pygame projects) only supports Python 2. It's not available in PyPi so pip install would not work.
Since you tagged this python3.5, I assume you want Python 3 support.
There is a ported version available from PyPi: https://pypi.org/project/python3-albow/.
This project is a port of Gregory Ewing's Albow project (https://www.pygame.org/project/338/4687) that is only compatible with Python 2 and is unsupported since 2014.
You should be able to install it using standard pip install
$ python3.5 -m pip install --user python3-albow
...
Installing collected packages: pygame, python3-albow
Successfully installed pygame-1.9.6 python3-albow-2.7.8
$ python3.5
Python 3.5.9 (default, Nov 24 2019, 01:35:13)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import albow
>>> albow.__path__
['/home/gino/.local/lib/python3.5/site-packages/albow']
>>>
Or, preferably, setup a virtual environment and install it there.

Python 3.6.5_1 installed in incorrect location (Mac OS)

I install python 3.6.5_1 using
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.r
and get
Warning: python 3.6.5_1 is already installed
The currently linked version is 3.7.3
You can use brew switch python 3.6.5_1 to link this version.
I use brew switch python 3.6.5_1 and get
Cleaning /usr/local/Cellar/python/3.6.5_1
Cleaning /usr/local/Cellar/python/3.7.3
4 links created for /usr/local/Cellar/python/3.6.5_1
Typing which python3, there is no output, which python gives
/usr/local/bin/python
and typing python3 gives
-bash: /usr/local/bin/python3: No such file or directory
typing python gives
Python 2.7.15 (default, Nov 27 2018, 21:24:58)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
How do I make sure that the installed python3.6.5_1 is installed at correct location and is the one referred to by python3?
Extra background :
I have been working with OpenCV for around 5 months now, following this installation guide for installing all requirements including python3.6. I was using this version of brew's python instead of mac's one. Recently I had to install FFmpeg, some errors came up, unwittingly I did something (I don't remember, probably unlinked python) and then I wasn't able to install FFmpeg anyway but also this destroyed my workspace. Right now I keep getting the following error everytime I open up terminal:
-bash: /usr/local/bin/python3: No such file or directory
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 and that PATH is
set properly.
I tried brew install python3 and that solves the above warning for virtualenvwrapper. But it installs python3.7.3, whereas I need python3.6 for tensor flow etc.

Why is my sys.prefix being set to '/usr'?

I'm trying to install python2.7 on linux from source on a system where I don't have admin rights. There is a version installed in /usr/bin but I want my install to be completely independent of this. I'm also testing builds with a newer gcc compiler so I want to avoid prebuilt packages like anaconda.
I downloaded python source and ran the following
./configure --prefix=$INSTALL_DIR --exec-prefix=$INSTALL_DIR --enable-shared --enable-optimizations --enable-unicode=ucs4
make
make install
where $INSTALL_DIR is where I want my install to go.
The configure and build ran without error, however, when I run the built python executable
$INSTALL_DIR/bin/python
and check the search paths it is still using /usr as the prefix:
Python 2.7.13 (default, May 31 2017, 17:58:17)
[GCC 7.1.1 20170503 (Red Hat 7.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/usr'
This means modules I install into $INSTALL_DIR aren't being found. Also running pip install XXX doesn't do anything if the module is already installed in the /usr space.
I'd like to avoid using things like $PYTHONPATH and $LD_LIBRARY_PATH so is there a way to set this up where sys.prefix and sys.path point to $INSTALL_DIR and know nothing about the system level install?
Thanks for your help.
Stop passing flags other than prefix to configre, and your python will give correct sys.path that stats with $INSTALL_DIR.
$ ./configure --prefix=$INSTALL_DIR

opencv python osx

(Step 1)
I'm trying to get openCV to run from python on my mac using the MacPorts install http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port, and also trying to follow The Petite Geek's guide:
sudo port -v install opencv +python26
It runs for about 10 minutes without errors.
(Step 2)
I download ctypes-opencv source and demo files. I navigate to the src directory and run:
sudo python setup.py install
I see like 50 lines almost all of the form: copying ... -> ..., which looks good to me. No errors here.
(Step 3)
I add export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib to the end of my ~/.profile.
(Step 4)
I open a new terminal to test my install. From my home folder:
$ python
Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv
>>>
Does not work.
I read somewhere that opencv installs python bindings with the default version of python for OSX, and I am probably running a non-default version, but this is not actionable information to me.
I struggled for a while with Python on Mac. Here is how I finally (and easily!) installed it. Remove all the things Python you have on there already. They will probably be located at /Library/Frameworks/Python.Framework and /opt/local/var/macports/software/py26*
First download Python with Macports.
sudo port install python27
Then make sure your system is using this version with:
sudo port select --set python python27
Next install numpy with:
sudo port install py27-numpy
Now install opencv:
sudo port install opencv +python27
Now edit your ~/.bash_profile with:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit ~/.bash_profile
or
open -t ~/.bash_profile
and add the line:
export PYTHONPATH=/opt/local/var/macports/software/opencv/2.2.0_0+python27/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH
or where ever your version of the cv.so file is hidden....
Now restart terminal and try:
%python
>>>import cv
I'm using Netbeans for opencv and python and it works really nice. Good luck.
$ brew search opencv
homebrew/science/opencv
$ brew install homebrew/science/opencv
after installed, there is warning:
==> Caveats
If you need Python to find the installed site-packages:
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
so, just do
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
If you notice the first line output when running python, you'll see that you're still using the Apple-supplied Python interpreter. Try installing and using the python-select package in MacPorts and then try the instructions again starting from step 2.
Also make sure you followed all of the steps when installing MacPorts so that /usr/local/bin is on $PATH.
Another "hack" I found during my struggles using CMake (but maybe the problem is the same with ports) : it appears that the python modules location has been duplicated on my Mac OS Lion, for a reason I can't explain.
CMake wants to put the "cv" module here :
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Whereas my default Python interpreter is looking here (thanks PyCharm for telling me) :
/Library/Python/2.7/site-packages
Moving both cv2.so and cv.py files to the second location, did the trick for me. I don't know if this is the cleanest way.
Hope it can help some googlers !

Categories

Resources