VS Code debugger not using virtual environment in WSL? - python

I'm currently learning how to make Django projects but the material uses the Linux command shell and the instructors use Macs. I run a Windows machine, so I have to use WSL and Bash.
I'm trying to use the VS Code debugger with a Python virtual environment created in a Bash terminal with WSL. I create my virtual environments by opening up a terminal in VS Code, entering the Bash terminal, then typing python -m venv venvname.
On the bottom right of VS Code there's something like this where I can click on the Python version number to change the interpreter.
If I then click "Enter interpreter path" then "Find" it brings up a Windows Explorer menu where I can direct it to a python executable, but it seems that virtual environments for Linux (created through a Bash terminal) don't have a python.exe. I have a python file with seemingly no extension that I am also unable to open.
I've tried using a venv created with Windows powershell in VSC just for testing purposes. After creating the venv, VSC gives me a notification ask if I want to use the newly created venv (I get no such prompt when creating venvs with bash) and confirming makes the Python version number also indicate that it's using a venv. The files also has an actual python.exe file but it comes with other problems. I can't run the Activate.ps1 script to enter the venv in VSC. I can activate the venv by running command prompt/powershell outside of VSC as administrator, but it seems to have issues installing older versions of packages when I try to install from file with pip install -r requirements.txt.

Related

How to make VSCode integrated terminal run the same Conda Python interpreter as Terminal.app?

I have miniconda installed with automatic base environment activation using /usr/bash in the Terminal.app. When I first open Terminal.app and run python the correct interpreter inside the base environment is run, say Python3.8.
Inside VSCode (without necessarily having opened it using something like code .) I have the integrated terminal, which opens bash as expected. The prompt is in fact the same as when I open Terminal.app: say (base) $. However, if I run python, MacOS's system Python2.7 is activated, instead of conda's Python.
The only way I've found to actually run the Python interpreter from within conda's base environment, automatically and consistently with Terminal.app, is to run conda deactivate, then conda activate again. In fact, if I search for Python executables in the environment, by typing python then clicking tab from within the first initialization of the integrated bash terminal, the results include executables that are only available within conda's base environment, which leads me to believe that I have that environment truly activated.
Furthermore, if I use the Select Python Interpreter in VSCode and choose the base environment, when I activate the integrated terminal it will run source ~/opt/miniconda3/bin/activate base, and even then if I run python, conda's Python interpreter is ignored, and I will get MacOS's Python2.7.
I have tried setting both python.pythonPath and python.condaPath options to the default base Python in settings.json, with no success.
Somehow VSCode is activating conda's environment, as expected, but is overwriting the python command to the system's default Python2.7, instead of the environment's default Python3.8. How do I get VSCode to run the Python interpreter consistently with the conda's base environment, like Terminal.app does?

Can we create a venv for python and use that venv in other folders in VSCode, Windows 10?

Operating System: windows10
editor: VSCode (installed extensions: python, Code Runner.)
Python installed on my machine: 3.9.0
1- My project is in this folder "D:\darsy\Python\Learning\Code with Mosh - The Complete Python Programming Course for Beginners 2019-4\11- Popular Python Packages\code\7- Web Scraping\PyCrawler2"
2- I have app.py in this folder
3- I opened this folder in VSCode.
4- I opened VSCode terminal and run this command:
D:\darsy\Python\Learning\Code with Mosh - The Complete Python Programming Course for Beginners 2019-4\11- Popular Python Packages\code\7- Web Scraping\PyCrawler2> pipenv install requests
5- so I have a venv in this directory: "C:\Users\Acer.virtualenvs\PyCrawler2-RWWuk_HY"
6- I changed my python interpreter in VSCode to this venv.
7- I write this code in my app.py file
import requests
8- in VSCode terminal, I run this command:
D:\darsy\Python\Learning\Code with Mosh - The Complete Python Programming Course for Beginners 2019-4\11- Popular Python Packages\code\7- Web Scraping\PyCrawler2> python app.py
9- I got this error: ModuleNotFoundError: No module named 'requests' .
why? I have installed requests package in my venv.
I think it's not specific to this package. any package I install and want to use I get this issue.
Thanks for your helps in advance.
Please select the virtual environment you created in the lower left corner of VS Code, and then use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, it will automatically enter the selected environment:
Then click the run button in the upper right corner of Vs code:
In addition, it is recommended that you use the green run button provided by the "Python" extension to run the code in 'Terminal'.
In order for your app to use the packages installed in the virtual environment, the env needs to be activated.
Activate it by running:
source myenv/bin/activate # linux
python -m venv c:\path\to\myenv # windows
where myenv is replaced by the name of your virtual environment.
You can tell that an env is activated because it will show up at the beginning of each line in your terminal like this: (myenv) user#DESKTOP-001:
After that, when you run python run.py your app will automatically use the packages installed in the env.
You can also check out the virtual environment docs
before running the program I should activate the venv, for this run:
pipenv shell

Why does my program run locally, but not in the virtual environment?

I'm using PyCharm. When I press the run button, my file runs fine:
But when I try to run the same file in my virtualenv, nothing happens:
My interpreter settings are set to the virtualenv, so what else could be wrong?
Mac users:
The answer was that python3 wasn't using the same site-packages as the
virtualenv; and thusly, running python3 run.py would not run.
Ensure that you're running the virtualenv's python binary instead of
the generally installed python3 binary.
Windows users:
There is no Python on Windows by default. And therefore any version that you've installed is just python (I guess). The recommended way to manage multiple python versions is to use the Python launcher. Source

Activated virtual environment using incorrect Python executable

I am working on a project through PyCharm. When I started the project, the project interpreter was a newly created virtualenv located in my project folder at /path/to/project_folder/venv and using base interpreter /usr/bin/python3.6.
When working in PyCharm, the Python Console seems to be using the right venv/Python executable etc. Running os.system("which python") returns /usr/bin/python.
Next, I activate this venv through my terminal (on Ubuntu 18.04) using the command source /path/to/project_folder/venv/bin/activate which works fine and shows me that it is activated with a (venv). However, if I run which python, it returns /home/user/anaconda3/bin/python.
Why is this occurring? How can I access the same Python interpreter from the PyCharm console through my Ubuntu terminal?
Same happened to me in a specific project.
Symptoms:
No executable taken from the venv; i.e:
which python3 from bash is not taken from venv
Python packages are not working from venv
Cause:
You renamed your project folder
Virtual environment PATH keeps old path to venv
It fails silently
Solutions:
Create a new venv and reinstall requirements, OR
Rename folder to its old name
Change PATH is not the solution. venv script creates some other routes in various files.
Note: Tested with python3 -m venv venv

How to manage (ana)conda with pycharm 2016.3 in linux

I would like to use Anaconda and the newest Pycharm 2016.3 together. I would like to be able to manage packages in settings->interpreter. If this is not supported, I would like to know the workflow of using these two together. According to another SO question, Pycharm 5 used to have a 'Create conda env' in the interpreter settings, but this seems to be gone now. I have tried:
1) Manually creating a virtual environment with 'conda create --name project numpy' and I add the interpreter ('~/anaconda2/envs/bin/python', the location of python for my created virtual environment. However, pycharm doesn't allow me to add any packages through settings->interpreter. Running an 'import numpy' through the console shows errors that are pointing to /usr/bin/python, not my virtual env python, and an error 'ImportError: cannot import name multiarray'. I'm not sure what package to add using conda from the cli, and the pycharm frontend doesn't add packages
2) I've tried the same as 1) but with my global anaconda python as the interpeter ('~/anaconda2/bin/python') and it doesn't seem to be able to connect to the console.
3) Creating a virtual environment through pycharm directly.
I would like to remove my default pythons (/usr/bin/python2.7/3.5 from the list of interpreters in pycharm) for debugging purposes but it won't let me and it seems to be showing packages that my anaconda virtual env doens't have installed.
Is there a way to manage my VIRTUAL enviornment in Conda using pycharm? If not, what steps do I take to make these two play well together assuming I can't manage it through pycharm interepreters settings.
This is the normal steps that i follow when i use virutalenv with PyCharm
I normally work on ubuntu
First, i always create a separate environment for every project using the command virtualenv "environment_name" from the command line.
Activate the environment using the command - source environment_name/bin/activate in ubuntu.
Suppose if i want to start a django project, i create the project using the command django-admin startproject project_name
Open this project in pycharm.
go to settings-> interpreter in pycharm. choose "add local" interpreter from the settings. It will open a pop-up. Go to the directory of the environment you just created and select the correct python interpreter you want to use.
now if you want to install a new package, you can go to interpreter settings and add package from the pycharm or you can fire up the command line, activate the environment and run pip install package_name. Once the package is installed, it will also show in pycharm.
if you are using Windows OS, use powershell to execute the above commands. The only difference will be in activating the environment. In windows, to activate an env use environment_name/Scripts/activate
EDIT:
Same goes anaconda environments also, the easy way is to manage the environment from the terminal and pycharm will show the packages changes in the interpreter settings.
Here is what I have been doing and it works great. I create a virtual environment separately and when I create a new project in PyCharm, I point the interpreter to the python from the virtual environment. An example is shown in the picture below:

Categories

Resources