Setting up virtual environments for python projects - python

I am trying to properly install python and set up virtual environments to make project development easier. Im stuck on how to install pip now. My question is this:
Do I need to install pip in a specific way if I'm trying to set up virtual environments?
Here's what I've done so far...
installed pyenv
used pyenv to install python version 3.7, 3.8 and 3.9
set global pyenv to be python 3.9
Now I don't know hot to set up pip.
I did this:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
and then:
python get-pip.py
..however when I go to verify pip was installed i get 'pip command not found'.
How do I properly set up pip so that I can create virtual environments and install packages for different environments?
Do I need to add pip to my PATH? If so, how do I do that?

You can create virtual environment directly from python with venv
(documentation: https://docs.python.org/3/library/venv.html)
Example:
python3 -m venv /path/to/new/virtual/environment
Activate the virtual environment
source /path/to/new/virtual/environment/bin/activate
Then you can install any package with
pip package-to-install

Related

Python virtual environment not installing packages in correct directory (python)

I have set up a virtual environment correctly have activated it and when I do "which python" it tells me the correct directory, but when I do pip install in the venv it installs the package in the default directory of the mac and not in my venv. I have tried using pycharm and installing packages with that, but it happens the same thing.
Edit:
Following I will do a walkthru of my steps, first I did python3 -m venv /path/to/new/virtual/environment, then I did source env/bin/activate, then I did which python and I got the intended directory, afterwards I did pip3 install numpy and I saw the installation process, then I did pip list and numpy was not there, I checked the directory manually and it still was not there. I retried all the same thing with pycharm with the same results.
Follow these steps in order -
In your current working directory :
python3 -m venv venv
source venv/bin/activate
(venv) pip3 install library_name
To check if libraries were properly installed or not :
(venv) pip3 freeze
and check if your installed libraries is displayed on the terminal screen or not.
To deactivate the virtual environment :
(venv) deactivate
This is due to permission issues
Run this command to change permission of your virtual environment folder
sudo chmod -R 777 venv
After activating your virtual environment
source venv/bin/activate
Then try to install Django by either Python3 or Python2
pip3 install django or pip install django
Then check the list of installed packages by either Python3 or Python2
pip3 list or pip list
NOTE: If you run sudo pip3 install django or pip install in virtual environment that is not writeable by default it will be installed outside of virtual environment
If you run pip3 install django or pip install in virtual environment that is not writtable by default it will be installed outside of virtual environment you will get OS Persmission Error
I have used pip3 and pip because you can install packages by either Python3 or Python2, thing to remember if you created virtual environment by Python3, use pip3 and if you created by using Python2 use pip.

How to create a Python 3 environment that has only standard lib, no extra packages

I'm trying to test how a package that I've created installs on a machine with a base Python 3 install, meaning no additional packages have been installed via pip, easy_install, etc.
How do I setup a virtual environment that acts like a clean install of Python 3? If I create a pipenv or virtualenv, then list all my installed packages, the 30 or so that I have are all still there. How can I make a Python environment that behaves like a brand new install of Python and has no extra packages?
This is in Raspberry Pi OS which has only one user, so using the --user flag in pip install --user might as well be a global install. I considered creating a new pipenv then pip uninstalling everything, but am worried that will break something.
Thanks for your help.
With virtualenv you would do :
virtualenv myenv -p python3.8
This would create a virtual environment in the myenv directory with python3.8 and the standard library only. You would activate this by doing
source myenv/bin/activate.sh
If you are using virtualenvwrapper, then you create the virtual environment by doing:
mkvirtualenv myenv -p python3.8
and activate it by doing:
workon myenv
I am sorry I am not a virtualenv user, however here is what I could suggest if you have conda installed:
conda create -n myenv python=3.6
Where myenv is the name of your environment and 3.6 the python version.
You can then enable the environment by doing:
conda activate myenv
A pipenv solution to this would be to create the virtual environment by specifying the version of python you wish to use.
pipenv --python <version>
This will create a virtual environment with the specified version of python and nothing else.
e.g.
pipenv --python 3 # Will use the first python 3 version found on your machine
pipenv --python 3.9 # Will use python 3.9 if it is installed on your machine
This works with --python 2 and --python 2.x also.
https://pipenv-fork.readthedocs.io/en/latest/basics.html#specifying-versions-of-python

Install different versions of python 2

My system is ubuntu 18.04.
I have a pre-installed version 3 and 2 of python.
which python3
/usr/bin/python3
python3 -V
Python 3.6.9
which python
/usr/bin/python
python -V
Python 2.7.17
I need to create several virtual environments, one for python 2.7.15 and another for 2.6. how can I do that?
There are different ways of creating virtual python environments. Three popular ones are
virtualenv
pipenv
conda
I personally like conda a lot.
virtualenv
Assuming you have pip installed, you get virtualenv with
pip install virtualenv
Once installed, you can change into a directory of your choice and create a virtual environment like this
virtualenv myenvironmentname
If you want to use a different python version in your virtual environment, you can specify this with the --python flag.
virtualenv --python=/usr/bin/python2.6 myenvironmentname
However, please note that this requires you to have the python version you specify installed in advance, virtualenv will not take care of that for you (have a look at Use different Python version with virtualenv for more details). So you'll need local installations of the versions you desire.
You then can activate the environment with
myenvironmentname/bin/activate
and go ahead to use pip to install packages, etc. Have a look at
pip freeze --help
to find out on how to make your environment reusable.
pipenv
pipenv combines pip and virtualenv.
You can install it using
pip install --user pipenv
Pipenv takes care of dependencies on a project basis
cd myprojectfolder
pipenv install
This will create a Pipfile which will track dependencies and a virtualenv (see https://docs.python-guide.org/dev/virtualenvs/ for more details).
To create an environment using a specific version, you can do
pipenv install --python '/usr/bin/python2.6'
or
pipenv install --python 2.6
Cmp. Set python version when creating virtualenv using pipenv. If you also have pyenv installed, the second form will prompt pipenv to attempt to install non-existing versions, afaik.
conda
Anaconda Python is a python distribution (with a focus on data science) that comes with its own package and virtual environment manager named conda. Anaconda Python is not available in the official package repository of Ubuntu 18.04 LTS but needs to be installed in another way (the official documentation can be found here: https://docs.anaconda.com/anaconda/install/linux/).
To create an environment with conda, do
conda create --name myenvironmentname python=2.7.15
In contrast to virtualenv, the environments are by default not created in the present working directory, but installed into the envs directory in your conda directory. conda will also take care to install the proper python version, that is at least as long as it is part of the default channel (see below).
You can then activate said environment with
conda activate myenvironmentname
As I wrote above, the python version you specify needs to be available from the configured conda channels. python2.6 however, was removed from the default channel. To remedy this, you can add the free channel back to your default list (see https://docs.conda.io/projects/conda/en/latest/user-guide/configuration/free-channel.html for more details):
conda config --set restore_free_channel true
After that you can
conda create --name myotherenvironmentname python=2.6
And switch between the environments as you like
conda activate myotherenvironmentname
For python3 python -m venv <your_virtual_enviroment_path> for python2 virtualenv <your_virutal_enviroment_path>
The to activate source <your_virtual_environment_path>/bin/activate. And to deactivate deactivate. Finally to check what is activated echo $VIRTUAL_ENV
I strongly recommend for one virtual environment for each project.

How to open virtualenv in python3?

I have both python 2.7 and 3.8 installed in my computer but whenever I would install virtualenv using pip install virtualenvwrapper-win and then open a new virtualenv using mkvirtualenv test it says Running virtualenv with interpreter /usr/bin/python2. Now I downloaded django in the virtualenv and got the outdated version 1.11. So now I am unable to import path from django.urls among other things.
Is there any way to install vitrualenwrapper with python3 interpreter?
Please help. I am trying to learn django and this is creating a huge hassle.
Virtualenv is already included in standard library of the Python3. You can create a virtual environment using the command below:
python3 -m venv venv
The second venv is the name of your virtualenv, you can name it as you want.
To use an existing virtualenv you should use the command:
path_to_your_venv\Scripts\activate.bat
https://docs.python.org/3/library/venv.html
You can create a virtual environment for any Python version by giving the python interpreter as an argument:
mkvirtualenv -p /path/to/python_binary test
virtualenvwrapper should be installed into the same global site-packages area where virtualenv is installed. You may need administrative privileges to do that. The easiest way to install it is using pip:
pip3 install virtualenvwrapper
or:
sudo pip3 install virtualenvwrapper

How to setup pip for coexisting Python 2.7/3.4?

I am using Anaconda Python 3.4 on a Windows 7 PC now. Recently I am trying to follow the instruction of the book High Performance Python to learn some profiling skills. To this end I need to use pip install to install several tools. Unfortunately, not all of them support Python 3, and I have to install Python 2.7 now.
Before installing Python 2.7, I would like to know how I should handle with such 2.7/3.4 coexisting system? How do I setup pip so that I could use pip install to install packages for different Python versions separately?
You can create a conda environment via:
conda create --name py27 python=2.7
and use this environment for your work with Python 2.7. Activate it with the command activate py27, going back to your root environment is just activate.
In the py27 environment you can install pip and all other packages you need.
pip is generally located at the Python27\Scripts and/or Python34\Scripts folder. If you wish to invoke pip directly in the command line, these folders should be in your PATH environment variable.
Now I would just rename pip.exe in Python34\Scripts into any other name, for example pip_for_3.exe. That way, when I install packages for Python27, I would just use:
pip install <package name>
and packages for Python34:
pip_for_3 install <package name>
Coexisting Python installations are not a problem, you just have to know which version is invoked every time. See this answer for the same idea.

Categories

Resources