How to select a Python virtual environment in LunarVim - python

I want to select a Python virtual environment for each of my different Python projects within LunarVim. In VSCode this would be "F1 -> Select Python Interpreter". How can I do this in LunarVim?

To select a Python virtual environment for a specific project in LunarVim, you can use the following steps:
Open the project in LunarVim.
Press : to enter command mode and type TermExec cmd='which python' and press Enter. This will show you the path of the currently active Python interpreter.
Activate the virtual environment you want to use for the project by running source /path/to/venv/bin/activate in the terminal. Replace /path/to/venv with the path to the virtual environment you want to use.
Again, run which python in the terminal to ensure that the virtual environment's interpreter is being used.
In LunarVim, press : to enter command mode and type let g:python3_host_prog = '/path/to/venv/bin/python' and press Enter. This will set the Python interpreter for the project to the one in your virtual environment. Replace /path/to/venv/bin/python with the path to the Python interpreter in your virtual environment.
After completing these steps, LunarVim will use the Python interpreter from the virtual environment you specified for the current project.

Related

Visual Code how can I activate the virtual environment automatically?

Maybe a simple question, but I cannot figure it out. I work within Visual Studio Code on a MacBook. Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.
In the terminal I see (.venv) (base) gaston#MacBook-Pro WebScraping %
When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.
My question is there a way to activate the virtual environment automatically when opening the directory?
VS Code Default Python Interpreter Path
You can do this in a number of ways. If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.
https://code.visualstudio.com/docs/python/environments
System Preferences->Settings->Python: Default Interpreter Path
You'll want to point it to your virtualenv Python installation, likely something like:
envFolder/bin/python3
VS Code Launch Configuration Python Interpreter
You can also set a specific launch configuration so that VSCode will still use your system Python instance by default; however, specific launch configurations will have the virtual environment specified.
https://code.visualstudio.com/docs/python/debugging
I did something within settings and now it works.
When I open VSC now it opens the directory and the right environment, just the way I wanted it.
Took me some while to figure what finally did the trick:
I opened settings Command + , typed env in the search box. Scrolled through the results until I saw:
Python > Terminal: Activate Env In Current Terminal
and checked the checkbox. That solved my problem.

Sublime build Python in venv [duplicate]

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.

Use Visual Studio Code with Miniconda on macOS

On my Mac, I installed Python using Miniconda and I have also installed various Python packages using conda. In the Mac Terminal app, my base conda environment is active and when I run Python it correctly uses the Miniconda version of Python as shown below:
In Visual Studio Code (VSCode), I have selected the Miniconda version of Python using the Python Interpreter setting. The correct version of Python and conda environment is shown at the bottom of the window:
However, when I use the built-in terminal in VSCode it still uses the macOS Python even though it shows the base conda environment is active:
I'm using the Python, One Dark Pro, and Jupyter extensions in VSCode. Also, the $PATH in the VSCode terminal and in the Mac terminal app returns the following:
# VSCode terminal
base ❯ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin:/Users/gavinw/miniconda3/bin:/Users/gavinw/miniconda3/condabin
# Mac terminal app
base ❯ echo $PATH
/Users/gavinw/miniconda3/bin:/Users/gavinw/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin
How can I configure the Visual Studio Code terminal to use the Miniconda version of Python?
After selecting the Python that comes with conda, the Python interpreter will be displayed in the lower left corner of VSCode, and then use the shortcut key Ctrl+Shift+` to open a new VSCode terminal, (or click "Terminal", "New terminal") it will automatically enter the selected environment and activate it automatically.
The terminal uses whatever python environment your configured shell command is set to. It is in no way linked to the environment of VS Code
However, If you attempt to run a Python file, it will open up the terminal with the full path to the specified interpreter
These details are laid out in the docs
The Python extension uses the selected environment for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a .py file open in the editor, and opening a terminal with the Terminal: Create New Integrated Terminal command. In the latter case, VS Code automatically activated the selected environment.
Also, if using code command from an activated session...
launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default Integrated Terminal.
Otherwise, launching the desktop app directly would also not be done from an activated conda environment
You may want to check the value in your settings for "python.terminal.activateEnvironment", but I believe this only applies when you actually run a .py file

Activate Anaconda Python environment *persistently* in Windows

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.

PyCharm with Pyenv

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.

Categories

Resources