How do setup virtualenv and dependencies in pycharm? - python

I am using a virtualenv on a remote machine and want simulate the same env on my mac so that I can use pycharm for further development.
My virtualenv is in the path , "~/venv"
I have created the ~/.pycharmc with following contents(as suggested in "How do I activate a virtualenv inside PyCharm's terminal?")
source ~/venv/bin/activate
/bin/bash --rcfile ~/.pycharmrc
works fine and creates the necessary venv, but it is not working in my pycharm environment(attaching image at the end)
What am I missing ?

Go to Settings > Project Interpreter > Python Interpreters > Add
Navigate to ~/venv/bin and select your python binary. PyCharm notices that it is an virtual environment and supports it completely. Make sure to select the added environment as your projects interpreter.

The problem was that I had copied the virtualenv folders from the remote machine. Once I installed all the dependencies and added it in the list of virtualenvs of PyCharm. It works fine now .

Related

Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10

My Flask App server is running but I have three imports that cannot be resolved.
I have tried:
reinstalling the imports individually
reinstalling requirements.txt
I configured VSCode Workspace with an extra path to my project folder (there is now a .vscode file within the root of my project folder where it was not before)
I have updated my venv path settings in VSCode
Here is my file structure:
- > .vscode
- > client *(React front end)*
- > data
- > server *(Python/Flask back end)*
- > app
- > venv
- config.py
- README.md
- requirements.txt *(this contains the 3 unresolved, along with several that are resolving)*
- .env
- .flaskenv
- .gitignore
- requirements.txt
Unfortunately none of these things have resolved my imports issue and my routes are still not working. Any ideas/suggestions?
Open the Command Palette (Ctrl+Shift+P), then select the Python: Select Interpreter. From the list, select the virtual environment in your project folder that starts with .env.
Run Terminal: Create New Integrated Terminal (Ctrl+Shift+` or from the Command Palette), which creates a terminal and automatically activates the virtual environment by running its activation script.
Install sqlalchemy and mongoengine with command pip install. Once installing them successfully, there will intellisense when you import them and no warnings shown.
Besides, the folder .vscode is to store Workspace settings as well as debugging and task configurations.
If you are using a virtual environment, and even after trying pip installing all the necessary libraries, you have to select the python interpreter that exists in the virtual environment folder.
(Ctrl+Shift+P) then search for "Python: Select Interpreter"
Click "Enter interpreter path" followed by "Find.."
Navigate to your project virtual environment folder
Go into "Scripts" folder and then select "python.exe" as the interpreter.
These steps allow you to select the right python interpreter associated with the project's virtual environment.
Open the Command Palette (Ctrl+Shift+P), then select Python: Clear Cache and Reload window.
Voila, all the import errors vanished.
I specified a path to the python interpreter I'm using within the settings.json file contained in the project repo's .vscode folder.
"python.pythonPath": "path-to-interpreter.python.exe"
Thanks to the following resource! https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b
I ran into this error after an upgrade of my local python version (brew -> manual install), even though the specified interpreter was already /usr/local/bin/python3.
CMD + Shift + P and re-selecting the [same] interpreter fixed the error.
In hindsight, I suspect that a restart of VSCode could have also fixed this. 🤷‍♂️
Perhaps VSCode is using the incorrect Python path for this reason. A base interpreter should be used instead of the vscode interpreter, if necessary.
After verifying my Python interpreter was sourced correctly in VS Code, I simply cleaned my env and reinstalled the project locally and the import errors disappeared. I personally had an odd VS Code/Pylance cache and reinstalling the dependencies and modules fixed it for me.
I was getting the very same error you have and none of the solutions presented here worked me.
I work on a windows machine. I use miniconda to manage my virtual environments. And when I'm coding I launch every program from the command prompt (cmd), including Vscode.
Even tough inside Vscode the python evironment was correctly settled I was getting the very same import error that you mention. The interesting part of it was that I wasn't having any problems to run the code, it was working as usuall. But when coding I wasn't able to see the functions related to the libraries with the import error.
The solution:
Activate the correct conda env for the project before launch Vscode from the command prompt.
Why does this happen?
I BELIVE that this happen because when you don't activate any conda env before launch Vscode. The base conda env is loaded as default. That generates the import conflict. (You can chek this out installing those packagues into the environment and running everything just as you have been doing)

VSCode does not recognize venv

When I create a new project and the virtual environment using the venv python package, VSCode doesn't recognize the new virtual environment. I follow the bellow instruction:
https://code.visualstudio.com/docs/python/environments
The command that I use in the VSCode integrated terminal is:
python -m venv .venv
The terminal that I use is PowerShell 7, But I tried the CMD terminal too.
After running this command, the .venv folder is created very well on the workspace and I checked its behavior on the terminal.
I tried conda package manager to create a venv and VSCode recognizes it. The problem is only with the venv Python package.
I also tried another Python version to create venv, But the problem still exists.
I read this question:
How can I set up a virtual environment for Python in Visual Studio Code?
I know how to add an environment manually, but I expect VSCode to recognize my environments automatically.
I added these lines to settings.json (Preferences):
"python.venvPath": "~/.venv",
"python.venvFolders": [
"~/.venv/Scripts"
]
I activated the venv manually using VSCode integrated terminal.
The problem still isn't solved.
This is a screenshot of my problem:
I know how to add an environment manually, but I want it to be automatic.
I tried VSCode on another PC, and it worked; It doesn't need anything to do except the presence of venv.
VSCode Version: 1.58.0
Python Extension Version: v2021.6.944021595
The "python.venvPath" should be set to the parent folder of the .venv folder.
Virtual environments located in the folder identified by the
python.venvPath setting (see General settings), which can contain
multiple virtual environments. The extension looks for virtual
environments in the first-level subfolders of venvPath.
But, you need not set that. The Python extension should find the venv environment, as it is just located under your workspace folder.
Virtual environments located directly under the workspace (project)
folder.
I tried it locally, it does not work too. It will only show the cached environment path, and the "python.venvPath" does not work either.
And there are some changes of the interpreter storage:
A VSCode internal storage is introduced which will now store the
interpreter settings in the workspace & workspace folder scope.
You can refer to here for more details.
So, it's recommended to select the environment path manually for now.
After writing this command in VS CodeTerminal
python -m venv env
You can simply use
.\env\Scripts\Activate
In the same directory path where you have created the virtual enviroment.
For some reason VSCode had uninstalled all my Python extensions including the main Python extension. After reinstalling them it was able to detect my virtualenvs again.
So I was having this same issue and I resolved it by,
1. Open Settings in VSCode.
2. Search for "python.terminal.activateEnvironment"
3. If unchecked/unticked, then check/tick for both "User" and "Workspace".
The problem is not with VSCode but the workspace settings in which I was working on.
Just changed that and Voila works like a charm.

no python at "C:\Users\AccountName\AppData\Local\Programs\Python\Python38-32\python.exe" error in VsCode

I was running the Django project without any problems. Until I reinstalled Windows and then reinstalled vscode! Now that I am running the Django project, vscode shows the following error:
Error: no python at C:\Users\AccountName\AppData\Local\Programs\Python\Python38-32\python.exe
This might be occurs if the python directory still in the environment variables path list.
First remove python entry from the environment variables path if exists.
Also there will be a chance for virtual environments in your project. If the virtual environment is setup in the uninstalled python version then you will get the same error message.
So, remove the virtual environments which is created under the uninstalled python. It can be done by deleting the virtual environment folder from your system.
Edit pyvenv.cfg
home = C:\Users\UserName\AppData\Local\Programs\Python\Python38-32
include-system-site-packages = false
version = 3.8.3
When I edited this file after checking the file path on my computer, it worked for me.
Based on the information you provided, it is recommended that you check the Python environment variables.
Since the Windows system is reinstalled, the environment variables are restored to the default settings. Therefore, please add Python environment variables:
Or you can reinstall Python and check the "Add Python 3.8 to PATH" option, which will automatically add Python environment variables.
You could also refer to : Python environment variables.
It's very simple
Delete your dbsqite3 file from your project folder
Open your project in CMD
install virtual environment pip install virtaualenv
virtualenv .
\scripts\activate
python manage.py runserver
and play with your code
you shared.
This works for Pycharm, It did to me
From your projects folder, delete the 3 folders i.e. .ide,venv, and the third folder i think.. do not touch the projects or scripts you created. After wards, go to Pycharm and configure python interpreter again. This will now enable you to run yo scripts now with no prob
make sure to install virtualenv befor install python
check wheather django is installed (if your are using django framework).
pip install django

Can't run Venv in VsCode

For the last 3 days, I have been trying to set up virtual Env on Vs Code for python with some luck but I have a few questions that I cant seem to find the answer to.
Does Vs Code have to run in WSL for me to use venv?
When I install venv on my device it doesn't seem to install a Scripts folder inside the vevn folder. Is this out dated information or am I installing it incorrectly. I am installing onto Documents folder inside my D: drive using python3 - m venv venv. The folder does install and does run in WSL mode but I am trying to run it in clear VsCode so I can use other add-ons such as AREPL that doesn't seem to like being ran in WSL.
For extra context I have oh-my-ZSH set up and using the ubuntu command line on my windows device. Any information will be helpful at this point because I am losing my mind.
venv folder in side D: drive
result
If you have the python extension installed you should be able to select your python interpreter at the bottom.
You should then be able to select the appropriate path
Run Set-ExecutionPolicy Unrestricted -scope process before activating virtual environment.
All the best
You don't have to create a virtual environment under WSL, it will work anywhere. But the reason you don't have a Scripts/ directory is because (I bet) you're running VS Code with git bash and that makes Python think you're running under Unix. In that case it creates a bin/ directory. That will also confuse VS Code because the extension thinks you're running under Windows.
I would either create a virtual environment using a Windows terminal like PowerShell or Command Prompt or use WSL2.

Fundamentals question: Django + Python Virtual Env + pyCharm and CLI -- are two separate virtual environments required?

Bottom Line:
I can get everything to work by configuring two separate virtual environments, one for pyCharm and one for the CLI. Is this really necessary or should I be able to use 1 virtual environment for both as I expected?
More Detailed explanation:
I'm very new so this is probably a facepalm type of question so i'll try to be terse.
I'm using Linux Mint, Python 3.6, django 3.0.3, and pyCharm 2019.3.1.
I can create a virtual env using venv in the cli and it works.
I can also create a NEW virtual env in pyCharm through the settings: Project: Interpreter interface, and it works, however it doesn't have venv as an option, it only has virtualenv.
But if I try to activate the virtual env i created in pyCharm from the cli (using virtualenv of course, not venv), it fails hard and thinks i'm using python 2.7 which isn't even installed on my system. If it try to point pyCharm at the virtual env I setup on the cli, I get an error 134.
Is this just a known/expected issue? Must I have two virtual environments for every project I want to access via both pyCharm AND the cli? And I assume this is unrelated but I also find it odd that pyCharm lists my interpreter as python 3.7, which also is not installed on my system. I'm using 3.6 alone.
Thanks for your time.
At this time, I'm going to just answer this as: you need a separate virtual env for each (pyCharm and CLI) as this approach is not difficult or time-consuming and I have not had any issues working in this way.

Categories