Visual studio code Python execution - python-3.x

I seem to have some path issue while running Python(v3.9.1) in Visual studio code .The Azure libraries/SDK are not picked up while running the python file.
Do I need to set the Azure SDK in the Python path ?
from azure.core.exceptions import ResourceNotFoundError
ModuleNotFoundError: No module named 'azure'

Nope, that's not needed. But for every project, it is recommended that you always create and activate a python virtual environment using:
# This command runs the Python venv module and creates a virtual environment in a folder named .venv.
py -3 -m venv .venv
# Activate the virtual environment
.venv\scripts\activate
A virtual environment is a folder within a project that isolates a copy of a specific Python interpreter. Once you activate that environment (which Visual Studio Code does automatically), running pip install installs a library into that environment only.
When you then run your Python code, it runs in the environment's exact context with specific versions of every library. You can create a requirements.txt file for the libraries you need, then use pip install -r requirements.txt.
Here is a snippet from a sample requirements.txt file:
azure-cli-core==2.11.1
azure-cli-telemetry==1.0.6
azure-common==1.1.25
azure-core==1.8.1
azure-identity==1.4.0
azure-mgmt-compute==17.0.0
azure-mgmt-core==1.2.0
azure-mgmt-network==16.0.0
azure-mgmt-resource==10.2.0
If you don't use a virtual environment, then Python runs in its global environment that is shared by any number of projects.
Refer to the Azure SDK for Python Developer docs for more information on configuring your local Python dev environment for Azure.

Related

Dynamically update virtual environment

I have created a virtual environment for my Python project and have installed my requirement.txt in it and every time my Python code ex executes it first activate my virtual environment and then run the .py code which is what I want.
I have an issue here - I want to do everything programmatically.
Below are the high level steps i'm looking for :
1)pipreqs requirements.txt using py code
2)check if .txt file has changed, create a flag(1,0)
3)check if virtualenv is created
2.1 - if not created, create and install 1 and run py code
2.2 - if created - check if flag=1 then re-install 1 and run py code
if flag=0 , just run py code
I'm struggling hard to achieve this. Could somebody help.
maybe
pip install -r requirements.txt
is a good starting point (if your paint point is to manually install all the required libraries). It installs all the libraries listed in requirements.txt.
If this is not enough putting this command in a bash / cmd file might be an option.

Getting a python script to acknowledge modules in a virtual environment

I have a script (personal, not for distribution) that works on one of my computers. I want edit it on another computer.
On the first computer, the script was created without a virtual environment. I want to start using them on the second computer.
I have these set up on the second computer.
Windows 10
VS Code 1.45.1
Python 3.8.3
Paths:
Python - C:\Python38
Virtual environments - C:\Users\<User>\Envs
Projects folders - C:\Users\<User>\Documents\python-projects
Environmental variable WORKON_HOME is set to virtual environments path
I copied the script to the project folder. After activating the venv with workon, I pip installed the external modules on the second computer.
I can see the modules when the virtual environment is activated and not when it is deactivated. I believe this means the virtual environment is working and the modules were properly installed.
However, I keep getting the ModuleNotFound error for the external modules. I've tried uninstalling and reinstalling the venv and the modules but I keep getting the error. To ensure it's not the computer, I deactivated the venv and installed the modules and the script worked. I even tried creating a new script with only import requests and I still get ModuleNotFound.
What do I need to do to get the script to use the modules in the virtual environment?
You can do this multple ways. Some are listed here.
1.using IDLE
>>> help("modules")
2.using PIP
$ pip list
3.Using Anaconda
$ conda list
Before doing this, activate particular environment.

How do we really link the PyPDF2 to a different version of Python?

How do we really link the PyPDF2 to a different version of Python? I do have several version of Python installed in my Mac (Python 2.6, 2.7, 3.6 etc.,) – itsraghz May 24 at 5:09
best way to do it is creating virtual environments in python as part of project source folders.
conventional folder name for a python virtual environment is .venv
to create a virtual environment:
from command shell cd to project source directory
run [python] -m venv .venv [python] should be the path of the executable file or python it self if referenced in environment variables. since there are multiple
python versions installed, using python executable path with desired
version would be ideal.
this way there can be n number of working environments with different python versions even with different library versions[i.e. for testing purposes]

Pycharm "Virtualenv Environment -> New Environment"

In the PyCharm you can run Virtualenv Environment -> New Environment
What exactly does this command do? It creates new venv or virtualenv? And what is better to use for the project (Django)?
In PyCharm you create a project-specific isolated virtual environment and its purpose is to manage settings and dependencies of a particular project regardless of other Python projects.
According to its website, Python 3.3+ versions use the 'venv' tool, instead of the third-party 'virtualenv.
I would use virtual environment for any python project, because you can keep your dependencies seperate and contains everything needed for your project to execute successfully.
For my money, you're better of using Conda. The main reason being that Pip and Venv are Python specific package and environment management tools. Whereas Conda also looks after non-Python dependencies. Which can be very important for certain libraries.

Getting Error: 'No module named flask' in VSCode even when I have installed flask

I want to debug an application using Python and Flask in VSCode. I have installed Flask and the app runs perfectly fine through cmd. But, when I try to debug it through VSCode, it gives the following error:
cd 'c:\Users\Aditi\CleanHandymanApp';
${env:FLASK_APP}='NewApp'; ${env:PYTHONIOENCODING}='UTF-8';
${env:PYTHONUNBUFFERED}='1'; & 'C:\Users\Aditi\envs\CleanHandymanApp\Scripts\python.exe'
'c:\Users\Aditi\.vscode\extensions\ms-python.python-2018.10.1\pythonFiles\experimental\ptvsd_launcher.py' '--client' '--host'
'localhost' '--port' '63143' '-m' 'flask' 'run' '--no-debugger' '--no-reload'
No module named flask
Can you please help me.
This error message can occur if you installed the python3 version of flask but Visual Studio Code tries to run your project with python2.
Make sure to select the correct version of python in the editor. This can be done by running the command Python: Select Interpreter from the Command Palette (Ctrl+Shift+P).
Activate your virtualenv and run
pip3 install -r requirements.txt
to reinstall all packages inside the venv.
For some reason VS Code thought I was missing all my packages the first time I debugged even though the app was running fine locally.
Sometimes you can get this error if you loaded Flask into a folder which has sub-files. For instance, if you loaded flask into the parent folder with the virtual shell instance but you're running your code in the child file (let's say parent is called crypto_files and inside that is a python source code file called blockchain.py ), then in order to get flask to run properly you'd have to run the file like this:
python crypto_files/blockchain.py
This allows your machine to see Flask running inside crypto_files but also run blockchain.py .
OR, it's possibly you could just reload Flask into the sub(child)file... blockchain.py and then you'd run it from within the subfile.
This complication is mainly due to modern "virtual instances" and shells which are basically like creating a virtual computer-machine inside your ACTUAL hard machine. Flask does this to avoid running everywhere, and since Flask is modular it allows each of your projects to run different modular configurations of Flask to suit each project precisely. The alternative would be awful: you'd have to load the fattest version of Flask with dozens of add-ons for each project, and so all your git and all your projects would have tons of extra code. Flask is built to be very small at the core to avoid this problem (too verbose!).
If you have installed flask in virtual environment, you should have activated it first.
source /path to env dir/bin/activate #in linux
workon 'name of env' #windows
Another option is add sys.path.append('d:/programas/anaconda3/lib/site-packages') in c:\Users\Aditi.vscode\extensions\ms-python.python-2018.10.1\pythonFiles\experimental\ptvsd_launcher.py
Being that "d:/programas/anaconda3/lib/site-packages" should be modified by your local python packages.
Use this command in the terminal instead of selecting run code:
python3 "insert your file name here without the quotes"
e.g.: python3 example.py
I had a variant of the issue mentioned by #confusius. I had installed both Python 3.9 and Python 3.10. I had added Flask to Python 3.10. I had been using one vscode workspace which had selected Python 3.10. I started another project in a different vscode workspace and it had selected Python 3.9 by default, which I didn't notice because I thought it would select the same Python I had already selected in the other workspace.

Resources