Problems installing Python packages into a virtual environment in Visual Studio Code - python

Although I'm enjoying developing in Python in Visual Code, I'm finding managing virtual environments and packages frustrating, and particularly am struggling with installing packages in the right place. Here's my sequence of steps, and the problem I then have - I wonder if anyone could kindly tell me where I've gone wrong? Or do I really need to include the full Python path?
So first I create a new virtual environment:
I can see that this works:
I then choose to use the Python interpreter in this new virtual environment (I can't quite see why I have to do this - surely this should happen as part of the activation process - but I can live with it):
At the bottom left corner of my screen, I get the reassuring fact that I'm using the right Python interpreter:
I then install a package (I've chosen requests more or less at random):
However, this is going in my default Python location. To get it in my new virtual environment, I seem to have to include the full path to the Python interpreter:
This can't be right, although it does work - I can now see the installed package:
Can anyone help please?

Personally I haven't had luck using PowerShell (due to permissions to run PowerShell scripts) so I use Command Prompt in VS Code instead.
For PowerShell, perhaps activating your environment using Scripts\activate.ps1 will work instead. From the docs at:
https://docs.python.org/3/library/venv.html
# PS C:\> <venv>\Scripts\Activate.ps1
StackOverflowExample\Scripts\Activate.ps1
It is a bit confusing in VSCode having an interpreter selected and a different Command Prompt/Power Shell terminal used to install packages into a virtual environment.
Another confusing point is running StackOverflowExample\Scripts\activate doesn't suggest you are doing anything is wrong.

I agree with Jason Cook. activate.bat used to activate the environment in Cmd, you should take Activate.ps1 instead of activate.bat.
But you need not activate the environment by yourself. The Python extension can choose the right one to activate the environment for you when you create a new terminal.
After you select the interpreter, you need to create a new terminal. In general, we take a shortcut of Ctrl+Shift+`.
And if you want to turn off this function, you can set this in the settings.json:
"python.terminal.activateEnvironment": false,

OK, thanks to Jason/Steven I have finally got the hang of what you should be simple, but isn't. Here's what I reckon is the easiest way to create and activate a Python virtual environment in Visual Studio code. Let's say I start with the Tutorial environment active, and want to create one called ForeignHoliday (we can but dream). Start by creating the new environment in the VS Code terminal:
This creates the environment:
However, it doesn't activate it, nor does it change the default Python interpreter to use the one for the new virtual environment. You can do both of these things in one go by choosing an interpreter - click here at the bottom left of the VS Code screen:
You can now select an interpreter - your new virtual environment (irritatingly) won't be listed yet, so you'll have to find it:
Choose to find your interpreter:
Double-click on the Python interpreter in the Scripts folder in your new virtual environment (the pythonw alternative doesn't invoke a terminal window, so most people should avoid this - see this SO article):
Now press SHIFT + CTRL + ' to start a new terminal window (NOT just CTRL + ', as this switches you to an existing terminal window). You should see this:
You can now install and import packages and they will all be in the right place! I wish I'd read this answer a few days ago ...

Related

VS Code does not change python environment

I am using VS-Code and anaconda environment for python interpreter. I select the exact anaconda base environment by ctrl + shift + ` and it also reflects in the downside panel of vscode. But, when I checked the python version it shows my system's default python environment 3.7.9. If you see the below screenshot than, the anaconda environment is with 3.8.3.
Please give me solution, Thank you.
Changing the version in VSCode does not change the the instance that your PS instance will use. Try doing where python to see where the V3.7.9 that your PS instance is picking up is. Then remove that version from the environment variables and add the path to the V3.8.3 instead.
Additionally you can do: To forcefully use v3.8.3
Specify python version in command
py -3.8 <command>
OR set PY_PYTHON environment variable to set which version to use.
Take a look at this for further help Python docs
after changing the environment, you can restart the vs code again. it might be changed now. if not, then try changing now againg by clicking the interpreter name which is displayed on left bottom of the vscode window
For those tried these steps and achieved nothing:
select different interpreter
reboot VScode
reinstall VScode Python extension and delete its folders
Probably you are working in the workspace and not in folder. You probably set interpreter at workspace level, that can't be used in one of the folders of the workspace. Try to open your folder separately from the workspace and select interpreter you want. This worked for me.
To check & change vs code interpreter:
In top left menu bar Click view
In the dropdown menu, Click Command Palette
Click Python: Select Interpreter
Choose & Click on your desired Interpreter
Another way to be sure to use anconda interpreter, open anaconda navigator and launch vs code from there.
original vs code How-To

VS Code doesn't recognize Python Virtual Environment Packages - It shows red underlines

I installed python virtual env. I use vs code. I imported scrapy in my code and vs code doesn't recognize the module. Actually, it works well when I run it. scrapy crawl tester
Just, vs code shows red underlines that mean "Unable to import scrapy" So this is just vs code issue, not venv or scrapy package install issue.
This code works well and actuallay, scrapy is imported without any issue. This is just vs code issue. Thank you.
You might be able to solve your problem by using CTRL+Shift+P to add "Python: Select Interpreter" to your project.
If you created a virtual environment and activated it as well
type which python on macOS/Linux,
type where python on Windows,
inside the activated terminal session.
(env) userpc#pc:~$ which python
/home/userpc/Desktop/foldername/env/bin/python
In VSCode press Ctrl+Shift+P, under >Select Interpreter paste the location you receive using the former command.
Once done, restart VSCode.
VSCode will also ask you to install pylint in your environment and if it doesn't you can do the same by activating you environment and typing
pip install pylint
inside the activated terminal session.
Ran into the same problem - selected the correct Python interpreter in VS Code, pip installed all the desired Python libraries but import is still underlined in VS Code.
What you need to do
What worked for me is to make sure that the linter that you are using in this VS Code instance (I was using pylint) is from the bin folder of the virtual environment, not somewhere else.
How you can do it
I'll use the absolute path to the desired virtual environment /User/ProjectFolder/env as an example.
To check that you meet the conditions stated in What you need to do, toggle the settings.json file in VS Code by pressing ⌘ + ,:
Make sure that the correct Python interpreter is selected. i.e. "python.pythonPath":/User/ProjectFolder/env/bin/python3.
Make sure that the linter (e.g. pylint) is located in that bin folder, not anywhere else. i.e "python.linting.pylintPath":/User/ProjectFolder/env/bin/pylint, NOT something like "python.linting.pylintPath":/usr/local/bin/pylint.
This means that you have to install your desired linter in the virtual environment.
Hope this helps.
I tried #cleon-w's answer which worked for me. (Thanks Cleon)
I was using pyenv on my Mac M1 (Big Sur) with vscode. vscode could not resolve the
paths to python that the pyenv provides, so I looked within the pyenv scripts to find the absolute paths
to the underlying python and the pylint files.
As #Cleon W says I ensured pylint and python are in the same bin directory.
Then edit VS Code settings.json to point to them directly (bypass .pyenv) and the imports could be found.
settings.json (vscode)
"python.defaultInterpreterPath": "/Users/USERNAME/.pyenv/shims/python",
"python.pythonPath": "/Users/USERNAME/.pyenv/versions/3.9.1/bin/python",
I have similar problem with Django. What solved my problem was:
create .vscode/settings.json file inside root dir for project.
add this json {"python.defaultInterpreterPath": "path to bin directory in root dir for project"}
Ctrl-Shif-P -> Python: Select Interpreter -> select one that said: Use Python from python.defaultInterpreterPath. If it's not in the list, hit refresh (circled refresh button on the top of settings box).

PyCharm not able to create conda environment

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.

Emacs and conda workaround

I'm using emacs and anaconda.
I have this in my init.el:
(setenv "WORKON_HOME" "/home/user/anaconda3/envs/")
And conda on my path:
# added by Anaconda3 installer
export PATH="/home/user/anaconda3/bin:$PATH"
but emacs can't find my conda environments, which I understand it is supposed to be able to do..
So, when I run C-c C-p to start a new session, and C-c C-c, it fails to import my packages which are installed in a conda environment, with ModuleNotFoundError.
Since I have added this to my path and it still doesn't work, I am trying to work around this, and still be able to run my conda applications from emacs.
I can open a shell in emacs with M-x shell, then source activate myenv, and run python.
I now want C-c C-c to copy into /this/ shell. How do I mark this shell buffer as a python process to send my file.py's text to on C-c C-c, rather than just a shell shell?
Update1
I've also looked at the following references:
https://emacs.stackexchange.com/questions/20092/using-conda-environments-in-emacs
How does conda-env list / conda info --envs find environments?
But neither package works for me. I still get, when I try:
conda-env-list
*Conda envs*
Produces a blank buffer.
And this for pyvenv-workon:
pyvenv-workon
Work on: (empty)
These environments very much exist, and it makes it impossible to use emacs as a python IDE if I can't run my code.
What I found works for me is to use the conda package from ELPA and set two of its configuration variables to point to my Conda directory. The following snippet does the trick in my .emacs:
(use-package conda
:ensure t
:init
(setq conda-anaconda-home (expand-file-name "~/miniconda3"))
(setq conda-env-home-directory (expand-file-name "~/miniconda3")))
conda-anaconda-home is the equivalent to the ANACONDA_HOME environment variable (i.e. contains all files of your Anaconda installation)
conda-env-home-directory - is the directory where your virtual environments get stored (within the envs subdirectory)
With this configuration I'm able to run M-x conda-env-activate and have access to all previously created envs.
Programs inherit the environment variables from the shell that spawned them. The way conda and virtualenv work is by overriding the shell's PATH variable. They do this so that the OS finds the new version of the app (conda's or virtualenv's) instead of the default one installed with the OS (Macs come with an ancient version of python).
So, what is happening here? If you start Emacs by double clicking on the OS icon it will inherit the default shell environment variables. So when you try to call a library that you installed with conda (or equivalently with virtualenv and pip), because you are using the default OS path, the OS is finding the default version of python (and crucially the default version's libraries). The default version of python is going to respond "I have no idea what library that is."
How to fix? One reliable way is to not start Emacs by double clicking on the OS Icon. Here is what I do most days:
1) start a console/terminal
2) switch to the conda environment `activate py37`
(or with virtualenv: `source .py37dev/bin/activate`)
3) start Emacs from that same shell that has the modified environment variables.
On a Mac its: `/Applications/Emacs.app/Contents/MacOS/Emacs`
(I use a installed version of Emacs on the Mac because the one that
comes with Mac is ancient).
On Linux and Windows the path to EMacs will be different but the idea is the same.
4) start a shell inside Emacs and you should see the shell looks the way it does
in your conda shell (or virtualenv shell)
here it what it looks like for me:
see how the version of python is not the default OS python? Its the one from the virtualenv + pip environment (conda works the exact same way, just the start envirmonment is a different command)
I tested the solutions given in the answers of Wojciech Gac, of Mittenchops and James Anderson.
While solution of James Anderson's solution is by far the easiest, it comes with a few problems:
First, you have to reactivate the environment in each shell process you spawn in emacs. There is also the possibility that emacs has a different pythonpath, therefor reluctantly using system python and not venv python.
The solution with conda.el is somehow weird. In Melpa it is listed as obsolete and with instructions of https://github.com/necaris/conda.el it won't recognize the environments on my certain machine.
On the same machine the solution with pyenv as mentioned in https://emacs.stackexchange.com/a/20093/28567 is working like a charm.
Therefor you only need to install with M-x package-install search for pyenv and then insert following two lines into .emacs:
(setenv "WORKON_HOME" "~/anaconda3/envs") ; /anaconda3 || /miniconda || wathever path your conda installation is located at
(pyvenv-mode 1)
This is my minimal solution to this problem:
create a batch file like this
conda activate <yourEnv>
python -i
set (local) python-shell-interpreter pointing to the batch-file
run-python as always (C-c C-p ...)
I know this is not exactly an answer to your question. But If you JUST want to run your code, open the directory in the terminal where your files are located (e.g. cd /Downloads/Chapter01/yourfile.py) . Then, activate the environment (conda activate *your env*) and pass this command (python yourfile.py). Your code will be executed within that environment by python.
I edit remote files with emacs and run code like this in another terminal window. You may wanna open the two terminal windows side by side while debugging your code.

How to tell VScode, on my Mac running high Sierra, which version of python to use

I use VScode to write and run python. I use it with a Jupyter extension. I have 2 virtual environments created using Anaconda. One py27, for python 2.7.13 and the other, py36, for Python 3.6.5.
I have a simple code which I edited to only run in python2. When I run VScode, the virtual environment I run it in does not seem to matter. VScode seems to run but I can't figure out how to make it use py36 (python 3.6.5) or py27 (python 2.7.13).
By using different commands in the command palette, or double-clicking on phrases in the bottom border of VScode I can eventually get it to run which version I want. But I have not found a reproducible method.
I have tried about 100 times with no luck. I thought I found a method that would repeat but I tried it again and it didn't work.
I know this question is very vague but it would take pages to explain what I've tried. I have about 5 or 6 settings which I've tried a plethora of combinations. One of the commands I tried is to select a python interpreter. I have several options but none seem to have an effect. Also, in the bottom border of the VScode window, there is the name of a specific python interpreter. You can click on this to select a different python interpreter. As far as I can tell this doesn't do anything. Actually, it's a hindrance because a user may think they are actually using that interpreter.
I've tried both py27 and p36 but I get no difference. If I can get visual studio code to start out in py27, I can make it change to py36 and back to py27. But how to make it start in py27 seems random. My virtual environments are in my home directory, /User/myname/py27 for example along with the VScode app.
The documentation at https://code.visualstudio.com/docs/python/environments explains how to select your virtual environment. Without knowing where you installed the virtual environments the best I can tell you is you can manually specify what interpreter to use with the python.pythonPath setting or if you have both virtual environments in a directory outside of your workspace folder you can specify the common folder with the python.venvPath setting.

Categories

Resources