How to set a user-local python interpreter as default on clusters - python

I got access to the server of my lab yesterday. I was trying to do some scientific computing on it with Numpy or so.
The problem is that the server has a SuSE linux(I never heard of such thing..) installed, and it's very difficult to install anything on it. So the administrator suggested that I should set my python environment locally since I did't have root access.
I have checked the default python interpreter on the server, it has python2.6 and python3.x. But I wish to use python2.7.x.
I followed some instructions to install python2.7 into /home/kevin/.local with something like this:
./configure prefix='/home/kevin/.local'
It works, but not easy, because I have to type
/home/kevin/.local/bin/python
everytime.
So, here is my question: what should I do to set the python2.7 I have installed locally as my default python evironment, so I can use
python test.py
to run my script. And when I use pip, it will install modules locally automatically.
ps: I have went through many of the threads available out there, but they only covers how to install modules locally. So I don't think they meet my purpose. And I also tried virtualenv. As I mentioned before, the default python version is 2.6 in usr/bin/python, so I can only get a python2.6 virtual environment locally, which is not what I want.
Since English is my second language, I'm not sure whether I have got everything clear. If not, please point out. It would be very kind of you to do so. And thanks for help!

If you already have python2.7 and virtualenv installed locally, you already have everything you need.
The piece of information you're missing is that you can specify which version of python you want to use when you create your virtualenv. You can use the -p option for that.
virtualenv -p /home/kevin/.local/bin/python my_virtual_env
Once you activate it, your python command will refer to the proper installation. Also, if you install via pip, your packages will be installed in /home/kevin/.local/
Virtualenv's documentation can help you better understand if you have more issues.

Related

corrupted python virtual environment after update

this winter I've been working on a Flask Application following this tutorial.
Today, hence after 3 months, I decided to work again on it, but all my set up seems corrupted.
The application started simply activating the virtualenv and calling flask run.
As for today, the command returns:
No module named 'flask'
and so for pip, pip3 etc. even if all these modules are in venv/bin.
My only guess is that since then, I updated python systemwide to Python 3.8.3rc1, which somehow became the default python in the venv as well, even if I was working in python 3.7. If that is the case, I wouldn't know how to fix this problem.
Do you have any suggestion?
thanks
When you created your virtual environment (let's call it v), two things happened:
v/bin/python was a link to your system Python 3.7
v/lib/python3.7 was created for packages installed to the virtual environment.
When you updated, the v link remained the same, but now it points to Python 3.8, which will look for a library directory named python3.8. Hence, your old Flask installation is invisible to the new Python.
Ideally, you wouldn't use your system Python for anything; install your own Python (under /usr/local/ or something), so that instead of upgrading to a new version of PYthon, you can simply install a new version along side it. Then your virtual environment can continue using whatever version of Python it was created with.
That said, you should probably just recreate your virtual environment from scratch.
Try to update the Pip
->> pip install --upgrade pip
and then install the falsk again
->>pip install flask
and lemme know if it works

How to convert an existing Django app to run in a virtualenv?

There are so many questions about this, and so many tries I've made and probably bungled...
But let me stick to the real problem: I have a fledgling Django app that I want to insulate from future changes to support software. I think putting in under a virtual environment is the answer. I'd like guidance, even just a pointer to the right Howto (one about migrating, not a fresh install).
My environment is Ubuntu 16.04.3 LTS, apache2, python3.5 and django 2.0. I'll be upgrading to the next LTS, which is why I want to insulate this app from changes.
Complicating matters is the fact that python2 and python 3 are both here, and pyhton2 is the default (what you get when you just call for "python". That makes things weird, for instance, because pip3 is uses the default python, so the output of 'pip3 freeze' is very different from what I get when I run it under python3, and I don't know the details of why.
What has failed in the past is my attempts to do it following guidance aimed at a freshly installed OS. What's more, probably because I did something wrong, pip3 lives in my $HOME/.local/bin/pip3. I don't know how to undo this.
If you have an existing environment that you wish to replicate inside of venv or on another machine inside of venv, then do this command inside your original environment:
pip freeze > requirements.txt
On the other machine or inside the virtual environment ( after having run path/to/venv/bin/activate ), do:
pip install -r path/to/requirements.txt
This should save and then restore your environment.

Pip not installing Python modules to proper directory

So I downloaded a module, but when I tried to use it in a program, Python threw the error:
"ImportError: No module named markovify"
When I go to try to change my .bash_profile, it shows this:
"# Setting PATH for Python 3.5
The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH"
I've been using 2.7.10 all this time, and Terminal defaults to it. (When I type in Python, that's the version it says I'm using)
I'm trying to get pip to install modules for python 2.7.10 to a different folder.
What's weird is this: I seem to have installed pyparsing with pip and it seems to work. I tried installing markovify and it throws this error message.
What am I missing?
When I go to try to change my .bash_profile, it prints this
That suggests you're using some tool to change your profile. Did you also use that tool to install markovify? You need to be clear about your environment when you have environment questions! :-)
If you installed markovify from the command line, I'd suggest opening a new terminal and running these commands, just to make sure you're working from where you think you are:
$ python --version
Python 2.7.10
$ command -v python
/usr/bin/python
The most likely problem, it seems to me, is that you got a bit tangled up and wound up doing something other than what you intended. From a fresh start, provided you're starting with the environment you want, pip should, you know, Just Work.
I'm trying to get pip to install modules for python 2.7.10 to a different folder.
That's possible to do, but are you sure that's what you want? Usually if you're using the Python interpreter you intend to, and haven't putzed with PYTHON_PATH & friends, when you run pip, it will install to its default location, which sure enough is where Python will look for it when you say to import it.
As an experiment, you might check if markovify was somehow installed for Python 3.5. Try
$ /Library/Frameworks/Python.framework/Versions/3.5/bin/python -c 'import markovify'
(You might also want to check that the Python interpreter is in fact in that folder and has that name.)
When you're working on more than one Python version, you should work on Virtual Environments:
Virtual Env on Hitchhiker's Guide

Emacs: Program named "virtualenv" does not exist

I encountered the following error message, when I was in python mode.
Error (jedi): Failed to start Jedi EPC server.
*** You may need to run "M-x jedi:install-server". ***
This could solve the problem especially if you haven't run the command yet
since Jedi.el installation or update and if the server complains about
Python module imports.
Then I tried M-x jedi:install-server. Then it complained that
Program named "virtualenv" does not exist
Now from the results I got from google, I added the following line to my emacs.d/init.el file, but still I am getting the same error. Please note that I am using Windows 7
(when (memq window-system '(mac ns))
(exec-path-from-shell-initialize))
Trying to set up jedi in emacs under Windows environment has caused me so much time and trouble already, I just want to complete this once and for all.
Ideally, I am looking for an answer that lists out all the steps I need to setup the virtualenv (on Windows 7) in emacs.
I had the same problem but on Windows 10. I got it working by these steps:
In terminal (cmd/powershell/..):
pip install virtualenv
After that, in emacs:
M-x jedi:install-server
And Emacs could install the server.
FYI, I'm running python3.5, pip version 19.2.3, emacs26.2, W10
I think it is clear that virtualenv is missing. So you need to do
just two things. With this way you don't bother with PATH variable and all
that.
install virtualenv however you feel comfortable, (I use conda package manager for everything python related and have the installation instructions for that, feel free to use pip or whatever)
conda activate yourenv
conda install virtualenv
conda list virtualenv #check if virtualenv is actually installed
Find where the virtualenv is located (as I use conda, I know the files are in bin folder in my installation)
and then show jedi where virtualenv is in emacs C-h v jedi:environment-virtualenv "~/path-to-conda-env/bin/virtualenv"). For more help look in the documentation in Configuration or
comment below.
M-x jedi:install-server in emacs
Test on a .py file.

virtualenv with all Python libraries

I need to get Python code, which relies on Python 2.6, running on a machine with only Python 2.3 (I have no root access).
This is a typical scenario for virtualenv. The only problem is that I cannot convince it to copy all libraries to the new environment as well.
virtualenv --no-site-packages my_py26
does not do what I need. The library files are still only links to the /usr/lib/python2.6 directory.
No I'm wondering, whether virtualenv is the right solution for this scenario at all. From what I understand it is only targetted to run on machines with exactly the same Python version.
Tools like cx_Freeze and the like do not work for me, as I start the Python file after some environment variable tweeking.
Is there maybe a hidden virtualenv option that copies all the Python library files into the new environment? Or some other tool that can help here?
No, I think you completely misunderstood what virtualenv does. Virtualenv is to create a new environment on the same machine that is isolated from the main environment. In such an environment you can install packages that do not get installed in the main environment, and with --no-site-packages you can also isolate you from the main environments installed modules.
If you need to run a program that requires Python 2.6 on a machine that does not have 2.6, you need to install Python 2.6 on that machine.
I can't help you with your virtualenv problem as I have never used it. But I will just point something out for future use.
You can install software from sources into your home folder and run them without root access. for example to install python 2.6:
~/src/Python-2.6.2 $ ./configure --prefix=$HOME/local
~/src/Python-2.6.2 $ make
...
~/src/Python-2.6.2 $ make install
...
export PATH=$HOME/local/bin:$PATH
export LD_LIBRARY_PATH=$HOME/local/lib:$LD_LIBRARY_PATH
~/src/Python-2.6.2 $ which python
/home/name/local/bin/python
This is what I have used at Uni to install software where I don't have root access.
You haven't clearly explained why cx_Freeze and the like wouldn't work for you. The normal approach to distributing Python applications to machines which have an older version of Python, or even no Python at all, is a tool like PyInstaller (in the same class of tools as cx_Freeze). PyInstaller makes copies of all your dependencies and allows you to create a single executable file which contains all your Python dependencies.
You mention tweaking environment variables as a reason why you can't use such tools; if you expand on exactly why this is, you may be able to get a more helpful answer.

Categories

Resources