Path to conda environment as a variable in script - python

I am using Spyder and I am working with a virtual environment created with conda create, which is located in C:/Users/username/.conda/envs/...
To deal with GDAL proj.db error (proj_create_from_database: Cannot find proj.db) I need to set:
os.environ['PROJ_LIB'] = "C:/Users/username/.conda/envs/test/Library/share/proj"
Because I am not writing the script only for me, I would like to write a line to find a path to the current conda environment and save it as 'conda_env_path' variable (in my case C:/Users/username/.conda/envs/test).
All the answers I found say about finding path to environment in anaconda prompt, not as a line of a script. Thank you.

Related

Tell Atom+Hydrogen to look in virtual enviromnment for packages

I'm completely new to this. I have set up Python3 and Atom and installed Hydrogen for Atom so I can run each line of my code and see the output.
I have set up a virtual environment and added packages to it.
My problem is that inside my Atom .py file, when I say import numpy as np for example, it tells me that the module is not found. So I think it is looking in some default place and not inside my virtual environment. Which makes sense as I don't know how to tell it to look inside the virtual environment.
I know that inside terminal I can load the virtual environment and then call the .py file from there and it will look in the right place. However that is not what I want to do. I want to be able to tell it to look in the virtual environment in the top line of the code and execute using Hydrogen, and then load the packages I want using Hydrogen, and then carry on with each line of code after that using Hydrogen.
Can someone tell me how to tell python to look in a specific virtual environment for the duration of the .py file that is being developed/executed?
For our purpose here virtual environments are just changing the search path of your interpreter.
Therefore if we want to search in a given virtual environment we can just add the path of this environment to our search path, which you can do in python by using
import sys
sys.path.append('/path/to/virtualenv')
The path to your virtual environment depends on how you configured it, but usually they are stored in a subfolder of your home directory called .virtualenvs, so this would probably look like
import sys
sys.path.append('/home/username/.virtualenvs/EnvName/')
# rest of code
Also note that this does not change your system path or pythonpath environment variables, and therefore only lasts for the duration of this python interpreter instance.

Incude FreeCAD in system path for just one conda virtual environment

I want to be able to import FreeCAD into my python scripts, but only in one conda virtual environment. Is there a way to do this without adding FreeCAD to the path at the beginning of each file? I am using Pop!_OS, which should behave like Ubuntu here.
I already found that you can import FreeCAD, but the source I found did so by appending the FreeCAD library location at the beginning of the file: https://www.freecadweb.org/wiki/Embedding_FreeCAD. It looks like you could circumvent this problem by modifying your path variable, and I was able to do so on Windows in my workplace. I just want to do this only for a particular conda virtual environment.
Ideally,
import FreeCAD
will work in a special virtual environment, but not in others.
As mentioned, I got the import statement to work on Windows already by adding FreeCAD's directory to the PATH environment variable. It worked with the default python in command prompt, which should be the anaconda installation, so I think it works in all virtual environments. On Linux, though, I cannot import FreeCAD in python even when I use
PATH=$PATH:/usr/lib/freecad-python3/lib/
which I got from "locate FreeCAD.so" . I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'FreeCAD'
It would be really nice to be able to run the same code on both operating systems, and have the PATH modification confined to one virtual environment.
Conda does not look for packages from the PATH environment. Check this answer for the details. But first check whether your package can be installed using pip or conda.
You might go to the virtualenv site packages dir and add the path to the freecad into easy_install.pth

Specify path to specific python executable using conda

I am using python and conda within ArcGIS Pro 2.2, but am running into some problems with virtual environments. I have cloned a copy of the Arcgis Pro environment (hereafter clone). When I activate clone in the home folder for the original ArcGIS environment (C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3), the python executable is located at C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe. However, if I activate clone in any other location, the clone looks to the clone's home folder and activates that version of python (located at C:\Users\RDCERWAW\AppData\Local\conda\conda\envs\gee\python.exe). This is causing problems because import arcpy works when I run the python.exe located at .\arcgispro-py3, but not when using the python.exe located at .\gee. Even when I modify the PYTHONPATH to include the path the .\arcgispro-py3\python.exe, the other location is accessed first. I could always start the clone in the .\argispro-py3 folder, but unfortunately spyder keeps looking back at .\gee.
Does anyone know how to code conda so that it look in a specific location first?
If you're using sys.path.append(<preferred path>), try using sys.path.insert(0, <preferred path>) instead.
From what I've read, Python searches each path in the order it appears in sys.path.

Can't access activate, deactivate, or conda inside a conda environment

So if I load a conda environment and want to use conda to install a package (or whatever else), I have to specify conda.exe.
What I expect to work:
(randomenv) λ conda --version
The system cannot find the path specified.
What actually works:
(randomenv) λ conda.exe --version
conda 4.5.4
Further complicating things, the following:
(randomenv) λ where conda
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\conda
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\conda.bat
C:\ProgramData\Anaconda3\Scripts\conda.exe
Is the first entry supposed to be C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\conda.exe and the environment got setup wrong or something?
Edit:
We're now in to usage breaking issues. I can't use deactivate, because deactivate doesn't have a .exe extension.
Per usual, where deactivate turns up:
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\deactivate
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\deactivate.bat
C:\ProgramData\Anaconda3\Scripts\deactivate
C:\ProgramData\Anaconda3\Scripts\deactivate.bat
Even more hilariously frustrating:
(randomenv) λ C:\Users\[USERNAME]\AppData\Local\conda\conda\endevs\randomenv\Scripts\deactivate
The system cannot find the path specified.
and then it gets even better:
(randomenv) λ cd C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts\
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts
(randomenv) λ ls
2to3.exe* conda* easy_install.exe* epylint-script.py iptest3.exe* ipython-script.py pip-script.py pygmentize-script.py pyreverse.bat symilar.exe*
2to3-script.py conda.bat easy_install-script.py idle.exe* ipython.exe* isort.exe* pydoc.exe* pylint.bat pyreverse.exe* symilar-script.py
activate* deactivate* epylint.bat idle-script.py ipython3.exe* isort-script.py pydoc-script.py pylint.exe* pyreverse-script.py wheel.exe*
activate.bat deactivate.bat epylint.exe* iptest.exe* ipython3-script.py pip.exe* pygmentize.exe* pylint-script.py symilar.bat wheel-script.py
C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\randomenv\Scripts
(randomenv) λ deactivate
The system cannot find the path specified.
How can't it find a file that's in the current directory?
Edit2:
Found the following issue on GitHub which says that exiting an environment requires that you activate root. That isn't working either, because activate can't be found just like all the other functions listed above.
This just happened to me. Here is what I found out.
Within the scripts folder of my environment were three scripts:
activate.bat
conda.bat
deactivate.bat
along with three more without the .bat extension that were bash scripts. In each case, all they did was a delegate to the globally installed copy of the same tool using an absolute path name.
When given a command, such as "conda" at the prompt, Windows will search for a .com, .exe or .bat file to execute, taking the first one it finds. Because activating an environment adds it's locations to the head of the path, the first instance windows will find is the .bat file within the environment, which will be executed.
In my case, the .bat files had the wrong absolute path, and were trying to delegate to a global copy that did not exist, hence the error message. Even if you were in the environment script directory, you would still get the message because it's not the .bat file that the message refers to, but the program the .bat file delegates to.
When specifying conda.exe, you are specifically asking for the .exe file, which means the .bat file will not be found and run, and therefore the global copy is found, which of course works.
I fixed it by changing the path inside each of the .bat files to the proper path to the tool, according to the output of where x, which fixed the problem.
I had the same problem. Interestingly, when I set up a fresh environment the activate / deactivate commands work fine.
I compared the script folder for this environment with the broken environment. The broken environment had activate / deactive files in the scripts folder just like yours, but the working environment did not.
By deleting the extraneous scripts (deactivate, conda, activate, .pt-postlink), the broken environment now works again.
I suspect something else I've done corrupted this folder. Possible a package I installed is the culprit? If I figure it out I'll post back.

unable to load "gcs.csv" file in gdal

This question might be repeat but I did not get answer.
I have write flowing code in python ide .
out_srs = osr.SpatialReference()
**self.out_srs.ImportFromEPSG(4326)**
It run fine but when i run it from application it cause an error as follows
Note - Error in line enclosed in 2 stars -----
"Unable to load EPSG support gcs.csv file check setting GDAL_DATA environment variable which point to gdal library contains EPSG.csv file"
I have done it but i still get this error. but this code run separately but not in application. This code is from gdal2tile module of gdal. i am using python 2.7.6 and gdal 1.10.0 I am unable to sort out what is the problem and where it is. Please suggest how to solve this.
GDAL needs an environment variable named GDAL_DATA that points to a directory with various data files, including gcs.csv. Learn more about it here.
To check if GDAL_DATA is set, and contains gcs.csv, and if this is readable, use the following snippets to check the application. This should be near the code that raises the error.
import os
import stat
gdal_data = os.environ['GDAL_DATA']
print('is dir: ' + str(os.path.isdir(gdal_data)))
gcs_csv = os.path.join(gdal_data, 'gcs.csv')
print('is file: ' + str(os.path.isfile(gcs_csv)))
st = os.stat(gcs_csv)
print('is readable: ' + str(bool(st.st_mode & stat.S_IRGRP)))
Anaconda / Miniconda users
The correct way to use either Anaconda or Miniconda is to activate an environment where GDAL is installed. For example, activate the base environment for Anaconda from Windows cmd.exe:
call %LOCALAPPDATA%\Continuum\anaconda3\Scripts\activate.bat base
Activating an environment triggers environment variables such as GDAL_DATA (and others) to be set, and often changes the command prompt prefix showing the environment name. These environment variables are unset/restored when the environment is deactivated.
conda deactivate
I was able to solve this issue by taking the following steps to set the GDAL_DATA variable in windows.
Find the folder where gdal data is stored
\Anaconda2\envs\gdaltest\Library\share\gdal
open windows command prompt and run following command with the location of your gdal data folder.
set GDAL_DATA=....\....\Library\share\gdal
Happened to me on MacOS Catalina (10.15.5) while playing with PyQGIS (QGIS 3.12). Just searched on Mac finder for gcs.csv which returned multiple results:
/usr/local/Cellar/gdal/2.4.2_4/share/gdal/gcs.csv
/Library/Frameworks/UnixImageIO.framework/Versions/F/Resources/epsg_csv/gcs.csv
/Library/Frameworks/GDAL.framework/Versions/2.2/Resources/gdal/gcs.csv
I stick with GDAL.Framework and just added this environment variable into my script:
import os
os.environ['GDAL_DATA'] = '/Library/Frameworks/GDAL.framework/Versions/2.2/Resources/gdal/'
Script is not complaining anymore.

Categories

Resources