6

I am currently using a shared Ubuntu machine which has python2.7 and multiple packages installed via pip.

$ python --version
Python 2.7.12

$ pip --version
pip 18.0 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

$ pip list
Package                            Version
---------------------------------- -----------
asn1crypto                         0.24.0
awscli                             1.11.101
backports-abc                      0.5
...
..
.

I want to install conda and have Python 2.7 and Python 3.6 environment.

How can I install all the packages currently installed (pip list) in both conda env (2.7 and 3.6) ? I am not concerned with package version. Fine to install the same version or latest version for each package.

Spandan Brahmbhatt
  • 3,774
  • 6
  • 24
  • 36
  • Do you want them installed _via `pip`_ in your conda environment, or installed via `conda` if it exists on `conda` or `conda-forge`, `pip` if not, or installed via `conda` always (generating a recipe on the fly if one doesn't exist)? If you have no idea, I think the second one is likely to be best, but it's your choice. – abarnert Aug 15 '18 at 18:34
  • Also, "fine to install the same version or latest version…" but not an earlier version, right? – abarnert Aug 15 '18 at 18:35
  • Prefer to install via conda. And ya, not earlier version. – Spandan Brahmbhatt Aug 15 '18 at 18:36

2 Answers2

8

Install same versions

First, get a list of packages installed via pip into a file:

pip freeze > packages.txt

Then install them using conda inside your two environments:

conda install --yes --file packages.txt

Install ignoring versions

pip freeze will spit out packages with versions. To remove them, run this instead:

pip freeze | sed s/=.*// > packages.txt

This way you will more likely succeed in installing them using conda without getting dependency conflicts.

Anticipating PackagesNotFoundError

If you have a lot of packages installed, conda might fail to find some of them. In that case, check out this question.

Andrey Portnoy
  • 1,430
  • 15
  • 24
  • Assuming my conda python version is 3.6, this will install all packages in 3.6. Is there any steps to copy the same packages to python 2.7 conda env ? – Spandan Brahmbhatt Aug 15 '18 at 18:39
  • This is definitely the right thing to do if you want something "semi-automated". It will install all the same versions of the same packages if they all exist on `conda`/`conda-forge`, and give you an error if any of them don't match. (At which point you can edit `packages.txt` to remove some version specifiers, or pull some of the packages out to install separately, etc.) – abarnert Aug 15 '18 at 18:40
  • 1
    @SpandanBrahmbhatt As the answer says, do this same command inside your two environments, and you'll get the same packages (to the extent possible) installed for both. – abarnert Aug 15 '18 at 18:40
  • @SpandanBrahmbhatt Presumably you have your different Python versions in different conda environments. Switch between them and repeat the second command. – Andrey Portnoy Aug 15 '18 at 18:40
  • Perfect. Thanks guys. I will accept the answer once it allows me to do so. – Spandan Brahmbhatt Aug 15 '18 at 18:41
  • @SpandanBrahmbhatt One last thing: This answer seems to implicitly assume that you're using a single Anaconda installation with conda environments for 2.7 and 3.6 (rather than two completely separate Anaconda installations). That's exactly what you _should_ be using, unless you have a good reason to do otherwise, but if you're not, the details may be slightly different. – abarnert Aug 15 '18 at 18:42
  • @abarnert I am using the same conda installation. Just creating environment for 2.7. – Spandan Brahmbhatt Aug 15 '18 at 18:43
0

Downloading conda and installing multiple python versions are given here and also you can find many important commands related to conda. https://github.com/Nitish1206/conda_setup_for_ubuntu

Install pip packages in conda via.

*while read requirement; conda install --yes $requirement;or pip install $requirement; end < requirements.txt*
Nandita Sharma
  • 13,287
  • 2
  • 22
  • 35
Nitish Jha
  • 23
  • 8