Python selected libraries - python

I am running RHEL5 , and i have python 2.4 installed on it.Please dont ask why the native version?
My question is that in my home directory i want to install python2.6 with selected packages only and also most importantly i dont want any of the library os or any other which deals with system calls or mail box preferences(sending mail to be prohibited).Is it possible to install a python version with selected libraries only.
So now on the system python 2.4 and selected libraries of 2.6 exists

This may not be quite what you're looking for... but the following steps will accomplish something similar to this (if you have root access). The below steps assume you are on i386 architecture.
1) Install Python 2.6 in parallel on your system (requires root). It is available at the EPEL repository:
$ rpm -i http://download.fedora.redhat.com/pub/epel/5/i386/python26-2.6.5-6.el5.i386.rpm
2) Install setuptools by running (again as root):
$ curl http://peak.telecommunity.com/dist/ez_setup.py | python26
3) Install virtualenv (http://pypi.python.org/pypi/virtualenv):
$ easy_install-2.6 virtualenv
4) From your home directory, create a new python environment:
$ cd ~
$ virtualenv --no-site-packages my-env
5) Activate the environment:
$ source my-env/bin/activate
You can now use easy_install to install whatever packages you want, remove anything you don't want from ~/my-env/lib/python26/site-packages, etc.
To let you know you are working in the virtual environment, your prompt will look like this:
[(my-env)you#host ~]$
One thing you have to be careful about is python scripts that start with #!/usr/bin/python, as they will not use the environment version. To ensure that the environment version is used, execute your script like this:
$ python your-script.py
Or you can specifically specify your environment version in your scripts:
#!/home/your-name/my-env/bin/python

Try ActivePython - it installs to an isolated directory (eg: /opt/ActivePython-2.6), and you can use PyPM to install third-party packages.

Related

Unable to upgrade from Python2 to Python3 (Ubuntu 19.04)

I had python2 installed in my ubuntu (19.04) and I wanted to get it upgraded to 3.7. I installed the 3.7 but still the version showed as 2.7. but I could run python3 and go to the console and python3.7 executable was available in /usr/bin. Did try all the tricks available in internet without any luck. then I decided to delete python2.7 executable from the /usr/bin and now I cannot even run pip as it tries to find the deleted 2.7 I guess. Getting the following message.
bash: /usr/bin/pip: /usr/bin/python: bad interpreter: No such file or directory
I don't know why it's still trying to find 2.7 like ex girl friend when 3.7 is installed and available in the machine.
Did you set up a path for python 3.7 in your .bashrc file? If not, try that and that should help.
Usually a path is something like:
$ export PATH=$HOME/Nek5000/bin:$PATH
First, run an update to make sure that there aren’t newer versions of the required packages.
sudo apt update --fix-missing
Next, you can try forcing Apt to look for and correct any missing dependencies or broken packages. This will actually install any missing packages and repair existing installs.
sudo apt install -f
(Source)
Also, always remember - never install directly in global environment. You should always create a local environment first(i recommend venv) as there are very high chances you can break something globally.
You can create a local environment by -
python3 -m venv tutorial-env
where tutorial-env is the name of your environment.
To activate this environment, you should run
source tutorial-tutorial-env/bin/activate

How can I change the Python interpreter in virtual environment (Ubuntu 18.04LTS)?

I always used Anaconda on Windows so far and could set up an environment while choosing which exact Python to use. E.g. conda create -n myEnvName python=3.7
Now, I want to familiarize with Ubuntu 18.04 LTS and use basic Python environments.
So I followed these steps:
Created folder in my home = ~/.venvPython
(a) I think I already had a 2.7 and 3.6 by default on the OS.
(b) I do not remember for sure, I think I had to do this sudo apt-get install python3-venv.
Created environment this way after CD'ing to .venvPython folder ran this: python3 -m venv venv1BigDataPgm2
source ~/.venvPython/venv1BigDataPgm2/bin/activate
Command python --version says: Python 3.6.9
Running whereis Python shows this:
rohit#rohitUb18043LTS:~$ whereis python
python: /usr/bin/python3.6 /usr/bin/python3.6-config /usr/bin/python2.7-config /usr/bin/python3.6m-config /usr/bin/python /usr/bin/python3.6m /usr/bin/python2.7 /usr/lib/python3.8 /usr/lib/python3.7 /usr/lib/python3.6 /usr/lib/python2.7 /etc/python3.6 /etc/python /etc/python2.7 /usr/local/lib/python3.6 /usr/local/lib/python2.7 /usr/include/python3.6 /usr/include/python3.6m /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
My doubts:
Can I specify a Python version directly while creating the environment like with conda?
How do I change this to some other interpreter instead of the 3.6.9?
Do I have to manually install a different Python first, then point it somehow?
Please guide me. Thank you.
Rohit
As far as I can tell the venv standard library appeared in Python 3.3 and was never backported to 2.7.
venv can only create virtual environment for its own version of the interpreter and the virtual environment directory can not be moved to a different location or be renamed. Python 3.foo can not create a virtual environment for Python 3.bar. So it is best to pick the wanted interpreter right from the start.
Since, as shown by the output of whereis python, you seem to already have multiple Python interpreters already installed, you should be able to do something like the following:
$ /path/to/python3.3 -m venv /path/to/my/venvs/venv33
$ /path/to/python3.8 -m venv /path/to/my/venvs/venv38
There seems to be a way to change the Python interpreter associated with a virtual environment (I have not tested it, not sure what the limitations are):
$ /path/to/python3.8 -m venv --upgrade /path/to/my/venvs/venv33
Alternatively use virtualenv which seems to offer a bit more flexibility, but is probably less efficient (its next major release, virtualenv 20, should bring a lot of improvements though).
Ubuntu and other Debian-based systems generally ship whichever Python version was current and deemed sufficiently tested when the release was published; after that, only security updates which preserve the version number but add patches are released (so you might get 3.6.9-123security4 instead of 3.6.9-5 or whatever was current when the release was cut).
If you want to run a specific Python version on one of these platforms, see if you can find an Apt source which provides this version for your system (Ubuntu has a soft underbelly of unofficial PPAs of various repute; Debian has backports) or install it from source yourself. There are add-ons like pyenv which let you do this rather easily, safely, and transparently.
There may also be an existing package which gives you a particular newer version; for example, you can do apt install python3.7 and apt install python3.8 on Ubuntu 18.04, but there are no packages for 3.5 or 3.9. Try apt policy python3.7 to see which specific minor version is available from the Ubuntu package archive.
An alternative to that is to always specify the python version you wish use when running a script.
python3.6 test_script.py
Usually, when I'm on Linux and don't need a specific python3 version, I create native python3 environments.
python3 -m venv myenv
source myenv/bin/activate
But if I need a specific python3 version, I do:
python3.9 -m venv myenv
source myenv/bin/activate
To use a specific python3 version with native environments, you have to install that version using the native package manager (eg. apt).

Anaconda Python: ModuleNotFoundError: No module named 'matlab'

I'm new to Python and Linux and want to install a MATLAB Engine for Python 3.6. I already followed the steps from Mathworks (http://www.mathworks.com/help/matlab/matlab-engine-for-python.html)
and tried also to use python setup.py build --build-base=$(mktemp -d) install but the error still appears. I have MATLAB R2018a installed and use Spyder 3.2.8 with Anaconda.
Does someone has an advice?
Thanks in advance!
If you're trying to install matlab engine in a conda environment, follow the below steps:
cd "matlabroot\extern\engines\python"
python setup.py install --prefix="installdir"
Eg
cd /opt/MATLAB/R2019a/extern/engines/python
python setup.py install --prefix="/home/nagabhushan/anaconda3/"
Now, matlab engine package is actually installed under the separate
anaconda3/lib/python3.6/site-package/matlab
So you need to manually move that matlab folder to the site-package folder your python is using. If you have multiple virtual envs, move it to the corresponding site-package folder of the virtual env you will run program.
Eg
anaconda3/envs/my_env/lob/python3.6/site-packages/matlab
References:
https://stackoverflow.com/a/41800724/3337089
https://stackoverflow.com/a/56553740/3337089
Edit 1 (03-Mar-2020):
I tried activating my environment and then simply installing matlab engine and it worked! I'm using Python 3.7
python setup.py install
Note that this initially gave error that error: You do not have write permission in build/lib/matlab/engine/, but changing the permission of build directory recursively worked: sudo chmod -R 777 build/
Following Nagabhushan S N answer,here, in order to install matlab/matlab.engine with conda (miniconda3) with Linux Ubuntu 18 LTS and Matlab 2018, I performed the following steps.
I have created a new conda enviroment (python36) with Python 3.6 interpreter, Python 3.6 is one of the supported version by Matlab 2018 (and you need to match the right Pyhton version or it'll install Python 2.7 libs), and I have directly installed the libs in the python36 env subfolder, in detail:
cd /usr/local/MATLAB/R2018a/extern/engines/python
python setup.py install --prefix="/home/myname/miniconda3/envs/python36"
e.g., import matlab.engine is now usable from the python36 environment.
P.s: the installation needs write access to the build subfolder (under usr/local...), so you need to go sudo and chmod -R +777 /usr/local/MATLAB/R2018a/extern/engines/python/build
P.s.2: by installing only in a base env, in my case, does not provide matlab.engine to work with the other conda envs too.. perhaps this is a normal, expected, conda behaviour.
After many tests I think I solved the problem. I will post the answer if someone has the same problems. As mentioned here:
https://stackoverflow.com/a/39759581/9834571
You can add an alternative python command. For me it worked when I vary the mentioned example as followed:
sudo update-alternatives --install /usr/bin/python python ~/anaconda3/envs/ 2
update-alternatives --display python
cd /usr/local/MATLAB/R2018a/extern/engines/python/
python setup.py build --build-base=$(mktemp -d) install

how can I install pygame on both python 2 and 3 on the same computer?

How can I install pygame on both python 2 and 3 on the same computer?
Its working with python 2 but i cant install it on python 3.
Use virtualenv. It will allow each project to have its own version of python and keeps all its python packages stored next to the project, rather than globally on the system.
cd into your project directory
Install a version of python 2 on your system
$ virtualenv p2env --python=2.7
Install a version of python 3 on your system
$ virtualenv p3env --python=3.5
You can how "activate" either of those environments.
$ ./p2env/bin/activate
You'll see your command prompt gets prefixed with the name of the environment. You're now running under that version of python. Additionally, any packages you pip install will be installed locally for that specific environment.
To deactivate the environment type the following.
$ deactivate
You should notice the environment name is removed from the command prompt. You're now able to switch to a new environment if you wish.
Make sure you add the environment directories virtualenv created to your .gitignore file.
/p2env
/p3env
Of course you can. Most linux distributions do so:
~$ python
python python2 python2.7 python3 python3.4 python3.4m python3m
~$ python
Debian symlinks python to python2.7 so you need to run python3 explicitly. On all other systems you can also have multiple versions in parallel in the same fashion.
Also there is a tool virtualenv which might help you in creating and manageing completely isolated python environments. (https://virtualenv.pypa.io/en/stable/)

Is there a way to install django with pip to point to a specific version of python in virtualenv

I have a system with CentOS installed. It currently runs python2.6, but python2.7 is also installed.
I want to run django 1.7, which is also currently installed. If I run django outside of a virtualenv, it is using python2.6 by default. I didn't install it myself.
What I assume is a way to get around this is to create a virtualenv. Which I've done, and used --python=python2.7. But when I create the virtualenv, and install a new django 1.7 in it (with pip), it still uses python2.6 instead of 2.7.
Since I'm doing this all through ssh, I'd like an easy way around it (rather than compiling from source, etc). Is there a way to specify that django uses python2.7 when I install it with pip in the virtualenv? Or what is the right way to correct this issue?
Here is what I have done:
ssh into account.
$ mkdir project; cd project
$ virtualenv env --python=python2.7
$ cd env
$ source bin/activate
$ sudo easy_install-2.7 pip
$ pip install django==1.7
Then I go into my python interpreter. The interpreter is running 2.7 and if I import django, it all works okay. But as soon as I run
django-admin.py startproject project_name
it is back to using 2.6.
Invoke django-admin.py like this python django-admin.py, in your activate virtualenv. Alternatively you can do /path/to/virtualenv/bin/python django-admin.py.
The best solution is probably adding a shebang to django-admin.py that looks like #!/usr/bin/env python which should use the python interpreter of your active virtualenv. See https://stackoverflow.com/a/2255961/639054
You could use pyenv, which will allow you to set your Python interpreter based on what directory you're in. I also use it with pyenv-virtualenv so that I get all the benefits of isolating my packages per application.
There's a bit of a learning curve, but it's worth it if you need to switch Python interpreters to match a production server for instance.

Categories

Resources