how do I get python to recognize modules installed in virtual environment? - python

I'm having trouble getting python in vs code to work properly. There were problems for a while (i dove right in with a workspace folder) so I decided to set up everything from scratch by starting a new project/workspace folder.
so far I've been having a few issues. The latest is this: despite creative a virtual environment as instructed on the vs code website, I can't get python to recognize that I've already installed the modules I want to use.
How do I fix this? (please see attached picture.
towards the bottom you can see that the virtual environment already has matplotlib, but python wouldn't recognize the module anyway

Create your virtual environment (e.g I used venv module to create a virtual env called TEST : $python3 -m venv TEST
From VS code go to Command Palette and type Python: Select Interpreter
And then you can choose the location of the desired interpreter (in my case the one from TEST virtual environment
You can always test your virtual environment from the terminal, e.g TEST env
Bash
From Cmd/Powershell normally you should run:
TEST\Scripts\activate.bat

Related

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).

Make VS Code terminal match debug environment on a Mac

I'm teaching a beginners python class, the environment is Anaconda, VS Code and git (plus a few extras from a requirements.txt).
For the windows students this runs perfectly, however the mac students have an existing python (2.7) to contend with.
The windows students (i.e. they have a windows computer), their environment when they debug matches their console environment. However the mac students seem to be locked to their 2.7 environment.
I've tried aliasing, as suggested here and here
alias python2='python'
alias python='python3'
alias pip2='pip'
alias pip='pip3'
I've modified the .bash_profile file
echo 'export PATH="/Users/$USER/anaconda3/bin:$PATH"' >>.bash_profile
Both of these seem to work perfectly to modify their Terminal environments, when launched externally to VS Code. Neither seem to do anything to the environment launched from [cmd]+[`].
I've also tried conda activate base in the terminal, which seems to have no effect on a python --version or a which python
They can run things using python 3, but that means that they need to remember that they are different to the other 2/3 of the students. It's othering for them, and more work for me!
The students are doing fine, launching things from their external terminal, but it would streamline things greatly if the environments could be as consistent as possible across the OSs.
Whilst they are complete beginners, they can run a shell script. They currently have one that installs pip requirements and vs code extensions.
Is there a configuration that will keep the terminal in line with the debug env?
In my opinion the best practice is to create Python virtual environments (personally I love using conda environments, especially on Mac where you stuck with unremovable old Python version). Then VSCode will automatically (after installing very powerful Python extension) find all your virtual environments. This way you will teach your students a good practice of handling Python zoo a.k.a. package incompatibilities. Terminal environments settings will be consistent with VSCode, without being dependent on unneeded any more aliases. Obviously, virtual environments are OS independent, so you will be more consistent and remove unnecessary confusion between different students.
The additional bonus of the virtenvs is that you can create one exactly according to your requirements.txt and switch from one to another with a single click (in terminal it takes two commands: deactivate -> activate).
You can read more about how to handle Python virtual environments on VSCode site
Given the aliases are run just once and are not persistent in .bash_profile, python targets the default interpreter rather than the expected conda python3 interpreter.
Try to symlink conda's python3 executable to capture the python namespace
ln -sf /Users/$USER/anaconda3/bin/python3 /Users/$USER/anaconda3/bin/python
This will create or update the symlink. Use the same approach for pip and pip3.
Python in vscode let's you select which interpreter will be used to run the scripts.
It is in settings under "python.pythonPath", just set it to point to the interpreter of choice.
It can be set on a project basis as well (which is how you ensure that a project that has a virtual environment will execute using that interpreter and packages), you just select Workspace in the settings pane and add the desired python interpreter there.

Python venv programmatically

We are trying to add venv functionality to our python project ie when one runs project an venv is created in project path and project runs on that venv.
For this to work we used vurtualenv's create environment method and activatethis.py the method creates venv ,
However when project runs it still uses host machine packages. Instead of using venv ones
Am I doing anything wrong..is it possible to do this??
PS: I have tried both site-packages true and false in create environment method .. however to no help
Adding more info:
Commands used are
venv_dir = os.path.join("path to project")
virtualenv.create_environment(venv_dir)
execfile(os.path.join(venv_dir, "bin", "activate_this.py"))
Also updated path environment variable to include bin of venv
After this I expect my code (after this) to run in virtual environment.
I am not using any ide and expect the code to create venv at runtime
Using python 2.7
Is there any other way to run project in virtual environment at run time without any ide in Ubuntu server environment.
You haven't provided enough information for us to come up with a solution to your exact problem, although I will do my best to help you out.
You should not be creating a venv every time you run the project, which it sounds like you are doing. This is essentially creating a new interpreter every single time you run your project. If you're using a decent IDE, such as PyCharm, you should be able to set this up properly through the project settings.
There are plenty of tutorials out there regarding setting up venv and virtualenv on your computer, and given that we don't know much about your system, your best bet is doing some further research.
Essentially, you need to
Create a venv for the project (and a new one for each project you make from here on out, unless you want to use the machine's interpreter)
Set the project interpreter to the venv you just created. Place any external libraries within ...\venv\Lib\site-packages
Hope this helps.

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.

Copy Python App to a new Machine?

I have a Python app running on windows that has imports for the following packages:
requests
json
psycopg2
I copy the entire project (I used Pycharms to write the app and import the packages) to a new machine and expected it would work. The new machine is also windows and I'm trying to run my script from the command line (i.e. no Pycharm on the new machine).
Instead, I get an error saying "ModuleNotFoundError: No module named 'requests'"
If I look at the project, I have the directories:
venv
Lib
site-packages
requests
What am I missing/doing wrong?
You have a couple of options here but first the problem. You are exporting your code base to a new machine without the required Modules installed on that machine and/or within your Python project's environment. After you have python installed on your new machine, you need to be sure to point your PyCharm Project to the proper environment.
File > Default Preferences > Project Interpreter
The window that appears on the right will contain a drop down menu labeled Project Interpreter. If you click on the drop down, it should reveal a list of the available Python environments on your machine.
Based on your description of your site-packages directory I would assume you do not have your interpreter pointed the proper environment on your new machine. With that said, you would be better served creating a new virtual python environment on your machine and installing each relevant dependency within that environment.
Take a look at this post here for your first best option on re-creating your old python environment on your new machine.
EDIT: I apologize for not reading the question more thoroughly before answering the questions. If this is running on a Windows machine you will need to double check the environment path python is using. It is very easy to install python at a different PATH than the command line environment is checking on a Windows box. If for example your PATH is pointing to a different version of Python and PIP is installing packages somewhere else this issue can occur. Double check your System PATH for python and which version the command line is running.
On the new machine you must source venv/bin/activate so your path environment variables are set properly. In particular, which python should say venv/bin/python rather than /usr/bin/python. Also, take care to conda env update or pip install -r requirements.txt so you'll have suitable venv libraries on the new machine.

Categories

Resources