Does virtualenv isolate python itself + python packages, or just python packages? - python

I just installed virtualenv onto my ubuntu 12.10 laptop. Am I supposed to install python itself into this vritualenv as well as all future python packages into this virtualenv?
Or is python not included in virtualenv typically?
when I type in 'which python' at the bash command line, I get /usr/bin/python, and it's python 2.7.3. Should I install, say python 2.7.5 inside virtualenv?
Thanks!

Virtualenv installs python, but it's installed in the bin directory of the virtualenv you created. Therefore you need to run it with ./bin/python.
You can also "activate" the virtualenv by running
source bin/activate
Which will put the virtualenvs bin directory first in the path (and do some other trickery I think) which will make the virtualenvs Python the default Python, so you can start it with just python. But this is not necessary.

Related

Multiple versions of Python in PATH

I've installed Python 3.7, and since installed python 3.8.
I've added both their folders and script folders to PATH, and made sure 3.8 is first as I'd like that to be default.
I see that the Python scripts folder has pip, pip3 and pip3.8 and the python 3.7 folder has the same (but with pip3.7 of course), so in cmd typing pip or pip3 will default to version 3.8 as I have that first in PATH.
This is great, as I can explicitly decide which pip version to run. However I don't know how to do to the same for Python. ie. run Python3.7 from cmd.
And things like Jupyter Notebooks only see a "Python 3" kernel and don't have an option for both.
How can I configure the PATH variables so I can specify which version of python3 to run?
What OS are you running? If you are running linux and used the system package panager to install python 3.8 you should be able to invoke python 3.8 by typing python3.8. Having multiple binaries named python3 in your PATH is problematic, and having python3 in your PATH point to python 3.8 instead of the system version (which is likely a lower version for your OS) will break your system's package manager. It is advisable to keep python3 in your PATH pointing to whatever the system defaults to, and use python3.8 to invoke python 3.8.
The python version that Jupyter sees will be the version from which you installed it. If you want to be able to use Jupyter with multiple python versions, create a virtual environment with your desired python version and install Jupyter in that environment. Once you activate that specific virtual env you will be sure that the jupyter command that you invoke will activate the currect python runtime.
I recommend you use pyenv a great tool for manage multiple python versions on the same system. Once installed you need to create a virtualenv, then activate the virtualenviroment and there you can install any libraries you want in a safe way.
By the way also come with an automatic installer pyenv-installer
Regards

Cannot execute virtualenv commands when using Python 2.7

I am trying to set up a virtual environment for a Django project, using Python 2.7. Both Python 2.7 and 3.6 are installed on my Mac.
I run the command, specifying the path to Python 2.7 virtualenv --python=/usr/bin/python2.7 venv and everything seems to work fine. However, when I run venv source/bin/activate it tells me venv: command not found.
Something is broken. I have read similar answers on Stack Overflow but nothing seems to be working. I have installed and uninstalled virtualenv with pip, tried running with superuser etc.
I guess you need source venv/bin/activate. source is a command, venv is the directory your virtualenv is located in.

Python virtualenv switch to 3.5 from 2.7

How can I switch from virtualenv that uses python 2.7 to python virtualenv that uses python 3.5?
Migration from 2.x to 3.x Python has nothing to do with virtualenv. If you already built your project keeping in mind a version change, then it will not be hard.
You can just download the Python35 and install it. After that, just execute
virtualenv -p /path to your Python35 directory/python.exe name_of_env
to create a new virtual environment for Python3.5. You can imagine the new virtual environment as a fresh Python installation with no third packages.
Please note that virtualenv just creates a new environment inside your computer with the Python version you specified in the -p parameter (or if omitted the Python version that is specified in your Path).
You can then install the desired packages for your project after activating the new virtual environment (./name_of_env/Scripts/activate) by executing pip install package_name
Although keep in mind that version migration is not the simplest thing. Many things can go wrong and especially the packages version support. Most of the packages support Python 3.x but not all of them.

Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x

I'm running on macOS 10.12, and I'm trying to install Django using:
pip install Django==1.10.5
I get this error:
Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x!
PYTHONPATH is currently: "/usr/local/lib/python3.6/site-packages:"
You should `unset PYTHONPATH` to fix this.
I have Python3 and Python2.7 installed with macOS on my system, but I really only want to use Python3 for pretty much everything. Pretty new to using / configuring Python, has anyone else come across this?
You shouldn't have site-packages in your PYTHONPATH at all; it's already going to be in sys.path for the appropriate version of Python, but having it in PYTHONPATH mean the other version of Python will try to use the incompatible modules found there. Figure out where you are setting PYTHONPATH to that (it shouldn't happen in any default setup, so you probably did the wrong thing in ~/.bashrc or the like manually), and stop doing it. If you can't figure it out, unset PYTHONPATH will remove it.
Since you want to use Python 3, use pip3 for package management, not pip.
You'll definitely want to use a virtualenv to separate your Django project from the system Python version. virtualenv creates a virtual environment - a dedicated version of Python just for your Django project - so you don't install any libraries with the system Python version, which is used for many other things. virtualenvwrapper makes working with virtualenv easy. pip install virtualenvwrapper is your friend. Here's how to install:
# Install virtualenvwrapper with the system Python version
pip install virtualenvwrapper
# Then, add these lines to your .bashrc, with the appropriate path to Python 3
# You can find the path to Python 3 with "which python3"
# virtualenvwrapper.sh is typically in /usr/bin or /usr/local/bin
export VIRTUALENV_PYTHON=/usr/local/bin/python3.5
source /usr/bin/virtualenvwrapper.sh
After that initial install, here's how to work with virtualenvwrapper:
# Make a virtualenv
mkvirtualenv my_project
# You're now in the environment for your project
pip install Django
# When you need to activate the virtualenv to work on your project
workon my_project
# Show the virtualenvs you've created - I use one for each Django or Python project I create
lsvirtualenv
Much more here: https://virtualenvwrapper.readthedocs.io/en/latest/
Good luck!

Changing Python version on OSX

I have python 2.7 installed on mac and I would like to use the CRYPTO module, but it doesn't support so installed home-brew and then downloaded python 2.7.9. I changed the path on ~/.bash_profile to /usr/local/bin
Yet, when I try which python it uses the default one.
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
I suggest you use virtualenv to create a virtual enviroment for your project, which provides you isolated Python environment specific to one Python interpreter.
Official Python package installation guide.
Quick example:
virtualenv -p /path/to/my/python venv
source venv/bin/activate
pip install pycrypto
If you need to activate virtualenv in your .bash_profile add line (. instead of source):
. /Users/you/venv/bin/activate

Categories