Make pyenv build python with different sqlite version - python

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.

Related

ipython attempts to automatically start from venv

I just installed ipython on root/global (i.e. just with apt install ipython ipython3 not inside a venv) on my Linux Mint 19.3 machine. However, trying to run it gives an error:
user#computer:/media/disk/dir$ ipython
/home/user/.local/bin/ipython: 2: exec: /media/disk/venv/bin/python3: not found
user#computer:/media/disk/dir$ ipython3
/home/user/.local/bin/ipython3: 2: exec: /media/disk/venv/bin/python3: not found
The error is that it is trying to load virtual environments automatically but they don't exist (anymore). I can't figure out why it does this.
Could not find a question about this, there are many about running from a virtual environment on purpose, but I want to just run it normally.
My python bins are in the usual place:
user#computer:/media/disk/dir$ which ipython
/home/user/.local/bin/ipython
user#computer:/media/disk/dir$ which ipython3
/home/user/.local/bin/ipython3
Running just python3 normally seems to work fine with regards to paths:
user#computer:/media/disk/dir$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/user/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.6/dist-packages']
>>> import os
>>> os.getcwd()
'/media/disk/dir'
System:
user#computer:/media/disk/dir$ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 19.3 Tricia
Release: 19.3
Codename: tricia
I don't have anything relevant set in ~/.bashrc.
More information:
user#computer:/media/disk/dir$ type -a python3 ipython3
python3 is /usr/bin/python3
ipython3 is /home/user/.local/bin/ipython3
ipython3 is /usr/local/bin/ipython3
ipython3 is /usr/bin/ipython3
user#computer:/media/disk/dir$ head -1 -- $(type -P ipython3)
#!/bin/sh
These look normal to me.
APT installs IPython 3 at /usr/bin/ipython3.
That means you have an extra IPython install at ~/.local/bin/ipython3, which references the virtualenv. If you still had the virtualenv I would say use pip to uninstall it, but since the virtualenv is gone, I think you can remove it manually.
And you might have another extra at /usr/local/bin/ipython3, but I'm not sure. That's where sudo pip would install it.
Now that said, in my experience with using IPython and pip on Ubuntu (similar to Mint), the default repos are always sorely out of date. The best solution I've found personally is to set up a virtualenv in my home folder and install IPython in it, then link it into my PATH in ~/.local/bin, just like the setup you had.

How to install gi module for anaconda python3?

python3 is my local Anaconda version of python, while python3.4 is the system one. I can import gi module with python3.4 (probably because i installed it with sudo apt-get install python3-gi) but python3 doesn't see it:
$ python3 -c 'import gi'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'gi'
$ python3.4 -c 'import gi'
$ python3 --version
Python 3.5.1 :: Anaconda 4.0.0 (64-bit)
$ python3.4 --version
Python 3.4.3
$ which python3
/home/kossak/anaconda3/bin/python3
$ which python3.4
/usr/bin/python3.4
$
How should i install gi for Anaconda python? Or maybe i can somehow import sysem-wide modules?
My os:
System: Kernel: 3.19.0-32-generic x86_64 (64 bit gcc: 4.8.2) Desktop: Cinnamon 2.8.8 (Gtk 2.24.23) dm: mdm
Distro: Linux Mint 17.3 Rosa
If you're using conda virtualenv for python-3, you can use
$ conda install -c conda-forge pygobject
in your virtualenv
You can read more about this on:
https://anaconda.org/conda-forge/pygobject
This is how you do it: (example for Linux Mint and python3)
First install gi module using your distro package manager. For Linux Mint it would be:
sudo apt-get install python3-gi
Then run your distro python to check where the module is located:
$ /usr/bin/python3
Python 3.5.2 (default, Sep 10 2016, 08:21:44)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> print(gi)
<module 'gi' from '/usr/lib/python3/dist-packages/gi/__init__.py'>
So in my case the module gi was installed to /usr/lib/python3/dist-packages/gi. Assuming you have your anaconda installed in /home/kossak/anaconda3, create a link to gi module in the proper folder:
ln -s /usr/lib/python3/dist-packages/gi /home/kossak/anaconda3/lib/python3.5/site-packages/
If you have conda virtual environment and want gi module to be available there, the path should be a bit different. Assuming the virtual env is called TEST:
ln -s /usr/lib/python3/dist-packages/gi /home/kossak/anaconda3/envs/TEST/lib/python3.5/site-packages/
and it works:
$ python3
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>>
If you want to perform a proper install (without soft linking) inside a Linux anaconda environment. Keep in mind that the errors may vary if you have not installed gcc previously (I assumed it was installed by default when I posted, however not everyone does so). Install it if you don't know very well what you're doing to avoid missunderstandings
Create or open your conda environment.
Attemp to install pygobject (don't worry, it will most likely throw an error):
pip install pygobject
In linux, it will promp the usual installation progress followed by an error:
(...) Please, try executing the following in your system:
sudo apt install libgirepository1.0-dev
Depending on your operation sistem or installed dependencies, the command name or package name may vary. Just follow the instructions and allow the system to install your packets. This step doesn't change anything, is just to give you the precise info of the package you need on your system. If you run this on Windows, it will ask you to install a specific version of Visual Studio. If you are in Windows, download the required Visual Studio from MS website, install it, reboot your computer and go to step 5 (in my case I never needed step 4 in windows, however, I'm not a MS expert.
Close your conda environment
conda deactivate
Next you need to install pygobject from conda-forge repository in your conda environment. You can add the repo to your favourite conda package manager or simply run the following command as root (it is important to be root). I did it outside the project, but you may do it inside:
conda install -c conda-forge pygobject
In my case conda was not in the path. I had is installed in:
/opt/anaconda3/bin/
You can run the following command from your normal user to find out where conda is:
which conda
Open the conda environment
source activate <your env name>
or the corresponding anaconda activate syntax (I never use it so I cant remember precisely)
Repeat the first step and now the installation wont fail:
pip install pygobject
OR if you specifically want to install gi you can run:
pip install pgi
the correct package is "pgi" NOT "gi"
As gi is a dependency of pygobject, everything will get properly installed. You can check it by running
python
>>> import gi
You may find the following usefull for Windows, although you may need to work it out a bit:
GStreammer python bindings on Windows
for me
conda install -c pkgw/label/superseded gtk3
worked

Linux using wrong python version

I have a Linux drive with no root access. It has Python 2.4.2 installed in /usr/bin/python directory.
which python
/usr/bin/python
I installed Python 2.7.8 in my local folder:
cd /usr2/steve/bin
ls
python2.7
If I still run Python it starts older version:
python
Python 2.4.2 (#1, May 6 2011, 13:26:21)
[GCC 4.1.2 20070115 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I have added /usr2/steve/bin to the PATH:
echo $PATH
/usr2/steve/usr2/steve/bin:/usr2/steve/local/mnt/workspace/steve/Python-2.7.8:/usr2/steve/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
How do I force the system to use the newer Python version as opposed to the older one?
You only have a python2.7 binary in your /usr2/steve/bin folder. You can make a symlink, then just calling python should work:
cd /usr2/steve/bin
ln -s python2.7 python
make your life simple by creating an alias. Are you using BASH (hopefully you are)? If so this is the syntax
alias pythonsteve='/usr2/steve/bin/python2.7'
(drop the = sign if you are running CSH). Next, run to verify
$pythonsteve -c 'import sys; print sys.prefix'
/usr2/steve/bin
Put the alias command in your shell user start-up file. On typical BASH installs this is the file ~/.bashrc. Note, when you manually edit the ~/.bashrc file you must either log out/log in, or run source ~/.bashrc for the commands to execute.
If you want to use python 2.7 you can specify it by appending it after 'python'
which python2.7

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

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.

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