0

I am relatively new to Python and trying to figure out how to set up my different implementations of Python. To run different packages, I have multiple installations of python 2.7 and 3.6, both 32-bit and 64-bit. For example, I run some GIS software that uses its own implementation of python (OSGEO4W) that I run from the installation location (32-bit Python 2.7). This python executable has a package (otbApplication), that doesn't really install from pip or conda. However, I mainly run python (also 32-bit Python 2.7) from Anaconda. When I am running from Anaconda, is there a way to have otbApplication and other libraries in OSGEO4W implementation of Python available?

user44796
  • 1,053
  • 1
  • 11
  • 20
  • Try creating virtual environments and then install different versions in different virtualenv. See here for details. http://docs.python-guide.org/en/latest/dev/virtualenvs/ – Anil_M Jan 18 '18 at 23:10
  • That seems like a great way to do it using pip and virtualenvs. I am looking for a way to do this in Anaconda. Can't seem to find the right way to do it, if there is a way – user44796 Jan 19 '18 at 19:35
  • Yes there is way as per official docs, pls check my answer for details. – Anil_M Jan 21 '18 at 04:03

1 Answers1

0

Once you have anaconda up and running you can use following process to install anaconda equivalent of virtual environment.

Make sure you are able access anaconda from your current folder

(base) C:\Users>conda -V
conda 4.4.7

Lookup python version available to be installed.
long list, truncated here

(base) C:\Users>conda search "^python$"
Loading channels: done
Name                       Version                   Build  Channel
python                     2.6.8                         5  defaults
|
|
|
python                     3.6.4                h6538335_1  defaults

Create virtual environment with selected python version

(base) C:\Users>conda create -n testVirtualEnv python=3.6.4 anaconda
Solving environment: done

## Package Plan ##

  environment location: C:\Anaconda3\envs\testVirtualEnv

  added / updated specs:
    - anaconda
    - python=3.6.4
|
|

#
# To activate this environment, use
#
#     $ conda activate testVirtualEnv
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Activate virtual environment Notice context changed from (base) to (testVirtualEnv)

(base) C:\Users>conda activate testVirtualEnv

Test to check python version in virtual env.

(testVirtualEnv) C:\Users>python -V
Python 3.6.4 :: Anaconda custom (64-bit)

Install a package in virtual env

(testVirtualEnv) C:\Users>conda install -n testVirtualEnv openpyxl
Solving environment: done

## Package Plan ##

  environment location: C:\Anaconda3\envs\testVirtualEnv
  added / updated specs:
    - openpyxl   
|
|
Executing transaction: done

Deactivate running environment
Notice context changed to (base)

(testVirtualEnv) C:\Users>conda deactivate
(base) C:\Users>

Remove and delete virtual Env

(base) C:\Users>conda remove -n testVirtualEnv --all    
Remove all packages in environment C:\Anaconda3\envs\testVirtualEnv    
Proceed ([y]/n)? y    
(base) C:\Users>

You can research further commands at official documentation site.

Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • Thank you for the tutorial. Unfortunately, what I need to create a link to a specific python interpreter. In my specific case, the interpreter is located at C:/OSGeo4W64/bin/python.exe. When I create the virtual environment as you specify, it creates an interpreter at C:\Anaconda3\envs\testVirtualEnv. Does this makes sense? – user44796 Jan 22 '18 at 16:42
  • You can use `-p` option to create virtualenv at specified location e.g. : `conda create -p C:/OSGeo4W64/testVirtualEnv python=3.6.4 anaconda` , As far as interpreter goes, conda utilizes its own and not from previously installed python versions, so sourcing them may not be necessary. – Anil_M Jan 22 '18 at 16:49
  • This only saves the virtual environment in the location. I am still unable to import packages that are located where the OSGeo4W64/bin/python interpreter is located. The method is similar to tutorial at this link https://my.usgs.gov/confluence/display/EGIS/Using+Anaconda+modules+from+the+ESRI+python+environment. Unfortunately, it is offline right now (probably because of US Government Shutdown) – user44796 Jan 22 '18 at 22:34
  • I believe you are looking to link/add existing packages to conda env. Check this link for possible solution: https://stackoverflow.com/questions/37006114/anaconda-permanently-include-external-packages-like-in-pythonpath and https://blogs.esri.com/esri/arcgis/2012/09/06/a-simple-approach-for-including-3rd-party-python-libraries-with-your-scripts/ – Anil_M Jan 22 '18 at 22:37