Using Sublime Text 3, how can I build a python file using a conda environment that I've created as in http://conda.pydata.org/docs/using/envs.html
NOTE: This will work for virtual environments created with conda as well as venv or virtualenv, you just need to activate it first to find the path to the python[3] executable.
A standard Python .sublime-build file looks like this:
{
"cmd": ["/path/to/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
All you need to do to use a particular conda environment is modify the path to the python or python3 executable within the environment. To find it, activate your environment and type (depending on the version you're using)
which python
or
which python3
on Linux/macOS, or
where python
on Windows, then copy the path into your custom .sublime-build file. Save the file in your Packages/User directory, then make sure you pick the right one via Tools -> Build System before building.
You may use a package called "Conda" from the package repository. Below is a detailed step by step guide for the same (using Windows 10 OS PC, however it should work on other OSs in a similar way):
Install Sublime Text 3
Press Ctrl + Shift + P to open up the Command Palette
Type "package" in the Command Palette search menu.
From the options, choose "Install Package Control"
Next after install, in the Command Palette type "Package Control: Install Package"
Search for "conda" to find "Conda" with the description "Work with conda environments in Sublime Text 3"
Navigate to "Preferences -> Package Settings -> Conda -> Settings-Default" to ensure the default settings such as Anaconda installation directory etc. are the same on your system. If they are not, open up "Preferences -> Package Settings -> Conda -> Settings-User", and copy the settings you'd like to update using the format shown in the default settings file as a template.
Once installed, a Conda build system will appear in the build sytem menu and conda's commands will be located inside the command palette
Choose the Conda build sytem by navigating to "Tools -> Build System -> Conda"
Create a test file and save it as test.py with the following code in it:
x = 1
y = 2
print(x + y)
Press Ctrl + B to build the file and see the output. If everything is working okay, you should see 3 as the output.
If you get an error such as error: [winerror 2] the system cannot find the file specified python, it may mean that Anaconda has different settings on your computer than the default settings. In that case you would need to pass your computer settings to Sublime Text in "Preferences -> Package Settings -> Conda -> Settings-User": 1) Change "executable": "~\\Anaconda3\\python" to the Anaconda python install location on your system, for example "executable": "Z:\\Anaconda3\\python.exe", 2) Change "environment_directory": "~\\Anaconda3\\envs\\" to the default environment directory on your system, for example: "environment_directory": "Z:\\Anaconda3\\envs", 3) Change "configuration": "~\\.condarc" to the path to conda's configuration file on your system for example configuration": "C:\Users\SantaPaws\.condarc"
Note 1: If you do not yet have a .condarc on your system, open "Anaconda Prompt" and type conda config --write-default. This would generate a .condarc file and save it somewhere either on your home directory (C drive) or the Anaconda directory. Search the file using Windows search and find its location. Refer to https://conda.io/docs/user-guide/configuration/use-condarc.html for full instructions.
Note 2: You may need to update the default %PATH% path variable in your system, so that it contains the directories for Anaconda. Type: echo %PATH% both in the "Anaconda Prompt" and the windows cmd prompt to see if these paths are the same, if not, you would need to update it in the windows system environment variable "Path". However, Anaconda recommends caution with doing this, as it can break other things.
In Linux Mint, I kept having trouble getting sublime to run python scripts using Anaconda's environment and Anaconda's installed version of python. I was running the following script to check which python was being used:
import sys
print(sys.version)
I followed THIS procedure on the Anaconda site, but I had to do one additional thing to get sublime to use the Anaconda environment and run python scripts using its python environment.
After choosing "conda" as my build system, I had to access the Command Palette (Tools -> Command Palette ...), and then I typed "conda", which shows you all of the options for controlling conda from inside Sublime, and I had to chose "Conda: Activate Environment", which shows all "conda" environments that have been created. I only had the original environment at this point, so it only gave me that one choice. I chose it, and then my script used the Anaconda environment, and its python version correctly.
FURTHERMORE, I noticed that if I wanted to switch to another virtual environment that I had previously created on my system before using Anaconda, I did have to activate that environment from inside Sublime first. I could then use the build system choice "Python + Virtualenv" to use that activated environment. Fortunately, the conda environment was still activated, and I only needed to use the build system choice of "conda" to switch back to it.
Coming across this same issue and using all of the information provided by the contributors to this post. My solution which provided the ease of switching virtual environments directly from Sublime Text 3:
Thom is correct to follow the guidelines of installing Conda for Sublime Text 3 provided by Anaconda's documentation: https://docs.anaconda.com/anaconda/user-guide/tasks/integration/sublime/
Once completed, open the command palette in Sublime Text 3 (pressing CTRL+Shift+p (Windows, Linux) or CMD+Shift+p (macOS)) and type conda to select Conda: Activate Environment. You will at first notice it only contains the base environment which means the settings for the Conda package is not pointing to the correct Anaconda environment path.
To find the location of your virtual environment, for Mac in terminal, activate a virtual environment as you would normally do by typing conda activate myenv (myenv is the name of any environment you have already created in Anaconda), when activated then type echo $CONDA_PREFIX which will provide you with the location of the environment. For me, I found the location is
~/opt/anaconda3/envs/
Once you have located the path, go back to Sublime Text 3, go to Preferences -> Package Settings -> Conda -> Settings. This will bring up the Conda package settings where you can change the path to the "environment_directory". Save, shut down Sublime Text and restart. Now you can have any python file open and change the environment at any time by going to Command Pallate, select Conda: Activate Environment and select the environment from all virtual environments you have created within Anaconda.
Related
I'm pretty new to programming, so maybe there's something painfully obvious that I've missed, but I've searched a lot around for solutions to this issue without finding any.
I just got a new computer and installed Anaconda and PyCharm for Anaconda. I got it to work fine on my old computer, but when I try to open PyCharm now and create a project, it gets stuck on the creation of the conda environment. Apparently there's a new conda update, so I tried to update to this one in Spyder, which seemed to work fine, but when PyCharm tries creating a new environment, it gets stuck and only displays
"Creating Conda environment"
"$ conda update -n base -c defaults conda"
Does anybody know what's happening? I've tried just waiting it out, but it doesn't seem to be moving forward at all from that point. I've also uninstalled both Anaconda and PyCharm with all their plugins and reinstalling them, but to no avail.
Thanks for any help or input!
Sincerely, confused first year student
It appears that Sophie was able to get the conda environment up and running.
However, other newcomers might come across a similar problem with regards to setting up Anaconda for use within PyCharm. Therefore, I provide a guide below that I recently (today) used in order to accomplish this on a fresh install on a new computer. For this I decided to use PyCharm Community Edition 2018.3 and Anaconda3 version 2019.07. I performed this on Windows 10 Enterprise version 1903. I have also performed this procedure on Windows 7 Home Premium edition (some time ago).
This will enable you to use PyCharm and Anaconda together, so that you can:
Know the prerequisite steps leading up to creating a conda environment, and become familiar with the steps following this action with using Anaconda with PyCharm.
Use the Terminal window in PyCharm with packages that are installed with Anaconda's version of Python. You can then use both regular python and conda commands to view, update or install packages. For instance, depending on your preference, you could use 'pip list' (python command) or 'conda list' to list installed packages.
Use the Anaconda python interpreter as the default Python Console in PyCharm
To accomplish this, please follow these steps:
Install PyCharm, and import any settings file you may have from previous PyCharm installation on other computer (optional). Assuming you are starting fresh, create a new project and set the (default) project folder location.
Install Anaconda. My install location was set to C:\ProgramData\Anaconda3. Note that 'ProgramData' folder is hidden by default. You should enable viewing of hidden folders in windows explorer, if you need to manually browse for it. Upon installation, I decided to have the options Add Anaconda to my PATH environment variable and Register Anaconda as my default Python 3.7 set as deselected.
Open Windows Explorer and paste in C:\ProgramData\Anaconda3 (or your install directory) in the address bar. This should take you to that directory. Navigate to the environments folder named envs. Select the address bar and copy that address. I got C:\ProgramData\Anaconda3\envs. You will need this later.
Open program named Anaconda Navigator (Anaconda3). You should see this from the list of newly installed programs. Alternatively, search for it and open it. This will initialize some stuff (unknown to me) that I have found necessary to be able to complete this guide. For the next steps we dont need this program running. You could close it if you want.
Decide on a name for the (ana)conda environment that we will create. I used py37 for simplicity. You only need to do this once for the python projects you will make. It is possible to make more environments if you should need that for some python projects in the future.
Open program named Anaconda Prompt (Anaconda3), preferentially as administrator. Opening as administrator may prevent complications that could occur on some systems. To do so, search for the program and then right-click it, and choose Run as administrator.
Enter this command: conda create -n py37 python=3.7 anaconda. It should prompt you to install some packages. Enter y to accept and proceed with the setup. This step may take some time to finish. Once finished, it may show you the needed command to activate the environment. Don't activate it just yet, ie. ignore that message for now.
Assuming environment path C:\ProgramData\Anaconda3\envs and environment name py37, run the following command (alter as needed to match your environment path and name): set PATH=C:\ProgramData\Anaconda\envs\py37\Scripts;C:\ProgramData\Anaconda\envs\py37;%PATH%
Run this command: conda activate py37. This will activate your environment.
Navigate to a folder you can easily access, such as the Documents folder on your machine. To do so, execute this command: cd "C:\Users\YourUsername\Documents".
Execute the following command: echo %PATH% > path_value.txt. This will export "PATH" values to a text file that can be found in the Documents folder as path_value.txt. Open this file and copy the content within. This will be needed in the following steps with setting up PyCharm for use with the Anaconda environment.
In PyCharm, navigate to Settings - Tools - Terminal. Check that Start directory field contains the path of your project folder, ie. such as C:/Users/YourUsername/Documents/YourProjectFolderName. Then, select the folder icon for the Environment variables field. Click the plus symbol, and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively.
In PyCharm, navigate to Settings - Project Interpreter - Add Python Interpreter - Conda Environment - Existing environment. Browse for C:\ProgramData\Anaconda3 (or your install directory). Select the file named python.exe. I chose to edit the Name field of this Anaconda python interpreter as Python 3.7 Anaconda.
In PyCharm, navigate to Settings - Build, Execution, Deployment - Console - Python Console. Check that the Python interpreter field contains Project Default (Python 3.7 Anaconda) or similar entry. The Environment variables input may be empty. Select the folder button next to it and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively. Exactly the same entries as was inputted in step 12.
[Optional] In PyCharm, navigate to Settings and search for the Run context configuration option. Right click it and select a desired shortcut method. Personally I use a keyboard shortcut which is set to Ctrl+Shift+Less. Once the rest of these steps are followed, this will allow you to run the current python .py file you have open, without having to manually set up configuration options for it (through Add Configuration option, next to green play button, as seen if you have no configurations set up from before). It will force PyCharm to use the default project interpreter (which now is the desired Anaconda-delivered Python 3.7 installation) for whatever project you have open. To use it, just open a .py python file with some code inside, click anywhere inside of it, and then use your shortcut option.
[Note: If you skipped step 15, go to step 17] Run your python file with the method from step 15. This should automatically set up a run configuration option for that file. You may get an error of missing packages. Ignore that for now.
Select Edit Configuration or Add Configuration in the dropdown box next to the green play / run button. Open the Templates tree, then select Python. Check that the Python interpreter option contains Python 3.7 Anaconda or similar. The Environment variables field may contain PYTHONUNBUFFERED=1. Regardless, select the related folder button and add a new entry with PATH and your path value (as found from the path_value.txt file) in the Name and Value fields, respectively. Exactly the same as we did in steps 12 and 14.
Restart PyCharm to re-initialize the Terminal and Python Console windows. Or manually close and reopen those windows.
You have now created and activated an (ana)conda environment, and enabled it's use from within PyCharm. You should also now be able to ie. copy your code over to a fresh new file, and then hit ie. Ctrl+Shift+Less to run the file without having to manually set a configuration file for it.
Hope this helped!
Sources: source1, source2, source3
In my Experience I just opened PyCharm as administrator and the error was gone!
If you use PyCharm to create conda environment, you can choose conda environment not virtualenv.
When you get the error like "can't get the /path/to/", you can open PyCharm with administrator.
If you use "anaconda prompt" to create conda environment, you can use command line like conda create -n py36 python=3.6. Then you can activate environment using conda activate py36.
I'm running Anaconda 3 with Python 3.6 on MacOS 10.12.
I created an environment using python 2.7 and opencv using conda create --name my-env python=2.7 anaconda(e.g. here). I activated the environment using source activate my-env and installed opencv. Both creation and activation seem to have worked: the anaconda3/envs/my-env folder exists and my terminal says (my-env) bob:~ alice'.
But how do I get Anaconda to use the new environment?
I tried:
starting the default Anaconda-Navigator.app, this just uses the default python path
changing the path in my ~/.bash-profile, but Anaconda still tries the default python path
starting the Anaconda-Navigator.app located in the my-env folder, fails with the OS error message "cannot be opened"
starting from terminal exits with the error message: FSPathMakeRef(~/anaconda3/envs/my-env/Anaconda-Navigator) failed with error -43.
Because I read somewhere that Anaconda 3 might not play nicely with Python 2.7, I tried the same workflow again, but creating an environment using python 3.5.4. Again to no avail.
What am I missing?
there a two possibilities to run a program in an env once the env has been created with conda:
Through default anaconda-navigator (the GUI of anaconda), there should be a menu on the left, with the following options: Home, Environments, Projects(beta),...
Simply klick on environments and choose/create a new one(see screenshot)
The "Applications on base(root)" can also be changed. If it does not display these options I recommend you to update conda.
Via terminal: activate the env as you mentioned earlier and then simply run the program you want, for example spyder, it should then use the right py version.
My default version of anaconda, the "root(base)" runs with python 2, therefore I added a few lines in my .bash_profile file to easily run python/spyder in either a default env or a specific one. maybe you can use something similar.
NONE='\033[00m'
YELLOW='\033[01;33m'
spy() {
if [[ "${1}" == "" ]]
then
ENV="py36env"
else
ENV=${1}
fi
source activate ${ENV} #enter env
echo -e "${YELLOW}CURRENT ENVIRONMENT: ${ENV} ${NONE}"
spyder #open spyder
echo -e "${YELLOW}EXIT ${ENV} ${NONE}"
source deactivate #exit env when spyder is quitted
}
You can now run spyder in the env "XXX" by entering "spy XXX" in the terminal. Or the default one by typing in "spy".
I can't figure out how to setup my Windows 7 machine so that it "always" finds my Python 3.6 Anaconda environment, which is not my root env. I don't want to have to do "activate ..." in a cmd window every time I want to use that env. And besides, there's a Spyder item in the start menu, for 3.6 (separate from default env), which won't work. I assume because of an env issue.
A robust solution greatly appreciated.
Dave
I had a similar issue where every time I opened anaconda prompt it started with 'base' as the default environment. But the issue was that I was mostly working in a different custom environment so I had to activate it each time I wanted to use it.
Here is the solution I found for autoactivating my preferred environment on a Windows 10 system:
Open anaconda prompt & use 'conda env list' to find the location of the environment you wish to use.
Go to the start menu, right-click 'Anaconda Prompt' and go to file location.
Create a copy of its shortcut
Open its properties & change the target to the location of your preferred environment.
Now every time you open anaconda prompt through this shortcut it will automatically load your chosen environment.
How to use Pyenv virtualenv's with Pycharm 2016.3? In the earlier version of Pycharm, I could easily set up local interpreter to point anything installed on my machine.
My first idea was to add .python-version file on the root of the project. I Pyenv virtualenv installed so this will activate & run the project with correct environment automatically. However, Pycharm still doesn't see the correct interpreter causing it to though import and syntax errors.
How can I select my local Pyenv in new PyCharm 2016.3 I used to be able to set the path as variable now I can only browse the path using drop-down menu. It doesn't seem to show hidden files like default path for pyenv ~./.pyenv/versions{project}.
In Pycharm version 2017.3, you can go to
Pycharm -> Preferences -> Project -> Project Interpreter -> <project_name> -> settings button on the right of the python interpreter text box -> Add local
This will open a new window with virtualenv Environment as one of the options on the left. On Selecting it, you will get an option to make a new virtualenv environment or use an existing virtual environment. Here next to the dropdown text box, you can click "..." and browse to your existing virtualenv created using pyenv and select it. It will select this virtualenv when you start terminal from Pycharm and also use the corresponding python interpreter as set while creating that virtualenv.
Get pyenv-virtualenv plugin for more project-specialized environments.
Then, create a new environment for project: (assume that we installed python-3.7.1 with pyenv and we'll use it)
$ pyenv virtualenv 3.7.1 projectName-3.7.1
This command generates folder for our environment.
Open pyCharm (v2018.3.1 used):
Create New Project > Existing Interpreter
Now you can type path of your environment:
~/.pyenv/versions/projectName-3.7.1/bin/python3
Then press Create..
That's all.
If there is already exists project:
File > Settings > Project: projectName > Project: Interpreter
Again, you can type path of the environment as like above. So you will see packages installed on this environment.
If you want to use same version of python and environment on the command line, then you must activate the environment with
$ pyenv activate projectName-3.7.1
command.
Note that pyenv virtualenv can activate that environment when entering the folder within the terminal through putting the name of it into your .python-version file as well.
For more command about pyenv-virtualenv you can look for reference sheet.
Personally, I made the best experiences with using pyenv and pipenv together. So far, I used separate commands for that, rather than using the pyenv-virtualenv plugin, but it should be supported with this hint as well.
My workflow to start a new project:
Create folder and switch into it:
mkdir new_project ; cd new_project
Set desired local pyenv version:
pyenv local 3.8.0
Create an empty pipenv virtual environment, using just that local version:
pipenv --python $(pyenv which python)
Now comes the tricky part: PyCharm is supporting Pipenv as an interpreter, but it doesn't recognize it automatically anymore after the initial interpreter selection (which happens at project initiation / first time opening of the project, automatically).
So - if you just created the new project folder (without PyCharm's .idea/ folder created yet), it will recognize the Pipenv-Virtualenv of the project just fine and set it as a project interpreter, automatically:
If there is already an .idea/ folder, it's not that easy, since PyCharm's GUI just supports to create a new Pipenv environment, but you still have an option:
Close PyCharm, delete .idea/ folder and reopen the project folder in PyCharm.
This will delete other project settings as well, but shouldn't be something too important for a fresh environment.
Open the folder in PyCharm again and it will recognize your Pipenv virtualenv.
You don’t mention what operating system you’re using, and it’s relevant here.
If it’s OS X or macOS, you can press Shift+Cmd+G in the file selection dialog (when you’re choosing the location of a new local interpreter) to enter a path manually. (This is a standard macOS shortcut that works in any native file selection dialog.)
After taking a lead from Mr. Judge regarding the use of pyenv, I stumbled on a way to introduce an interpreter from pyenv to an existing PyCharm (2020.2.2, if it matters) project without blowing away the .idea directory.
Prior to using any other Environment type (Pipenv, Poetry, etc.), first open the Virtual Environment option:
Select Exiting environment and then navigate to one of your pyenv shims using the […] button to the right of the Interpreter: drop-down. Then click Make available to all projects.
You can then go to the Pipenv or Poetry Environemnt (Plugin) to reference that introduced interpreter now.
I have just installed Anaconda on my computer because I need to use Numpy.
Well, when I use python I for some reason have to be in the same folder as python.exe and, of course, now that I want to use Anaconda I have to be in the Anaconda3\Scripts folder where python.exe isn't. This is a nightmare, how can I use anaconda with python on a windows computer? Why does it have to be so complicated?
I think you are referring to the command-line use of python?
If you have admin priviliges on your machine you can add python to your environment variables, making it available in the console anywhere. (Sorry for different spellings, I am not on an english machine)
Press Shift+Pause ("System")
Click "Advanced System Options"
Click "Environment variables"
In the lower field with "System variables" there is a variable called PATH. Append the complete path to your python.exe without the file to that by adding a ; behind the last path in the variable and then adding your path. Do not add any spaces!
Example: C:\examplepath\;C:\Python27\
When you install anaconda on windows now, it doesn't automatically add Python or Conda to your path.
If you don’t know where your conda and/or python is, you type the following commands into your anaconda prompt (it comes when you install anaconda)
Next, you can add Python and Conda to your path by using the setx command in your command prompt.
Next close that command prompt and open a new one. You should now be able to use the python command. To do this you open a command prompt and type
python nameofPythonfile.py
Source: https://medium.com/#GalarnykMichael/install-python-on-windows-anaconda-c63c7c3d1444
To be able to do that in the command line you just have to add Python and also the Anaconda3\Scripts directory to your system path.
Here is a good tutorial on setting your path in Windows:
http://www.computerhope.com/issues/ch000549.htm