Getting a python script to acknowledge modules in a virtual environment - python-3.x

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.

Related

Can't import modules in VSC but they have been install via pipenv

My operation system is macOs Ventura
This problem happens as i think only with VSC, becuase opening same folder via PyCharm all is importing successfully.
[Screenshot of Error, pipenv --venv, pipenv --wheree and pipenv --graph]:(https://i.stack.imgur.com/PR3Vh.png
I have created directory (folder), created local virtual environment via pipenv install django and active its via pipenv shell. Imports working good ... but just in PyCharm.
I have tried to reinstall VSC — not helped.
I have assumed that in sys.path wasn’t written this path /Users/sheenaz/.local/share/virtualenvs/Polls-njzI0GZr/lib/python3.11/site-packages — it was.
I have tried to reinstall pipenv and django — not helped.
As you can see that interpretator has entered correctly, but still doesn’t work (modules not importing).
I want that my modules will able to importing, can sombedy help?
I know answer!
At first it's a problem not with VSC or pipenv, It's just a problem with Code Runner extension that I use.
So, by deafault Code Runner python executor map is python or python3, you need substitute this by your virual environment executor or inerpreter.
To fix this go to the settings.json or to VSC code and in settings search type Code-runner: Executor Map. Now you in settings.json either or again).
Search for key "code-runner.executorMap" in dict and as you have finded then search key "python" and by default you will get defeault value like "python" or "python3" maybe. You need to change this value to your python virtual environment executor map (interpreter).
In my instance it is the pipenv interpreter — /Users/sheenaz/.local/share/virtualenvs/PLPY-ELrCBZFZ/bin/python3
Install specific debugger in VSCode. In my situation, debugger for OpevCV:
Because in Pycharm you install package with everything needed, while in VSC you need to additionally install the debugger.
Same problem on MacOS Ventura, had no problem running the same environment on MacOS Monterey, where imports work. Here assumed it's a path issue, after solving dependencies
export PYTHONPATH=/path/to/dir:$PYTHONPATH
then activate imports, by specifying both interpreter and path in VSCode (I have anaconda on Ventura and Python through Brew on the Monterey setup). Both now work.

VSCode switching multiple pipenv environments

In my workplace we manage multiple python flask projects and all of them are configured in the VSCode workspace. Each of the projects are separate folders and each of them has .venv folder created under it. We had a windows system earlier with which i'm able to run an application in VSCode (which starts with the venv activate automatically), and when we stop that application and run another application (different project with different venv) it automatically changes the application folder path, exiting previous venv and activating the current one.
Recently we shifted to MAC and this particulat auto switching to different venv is not happening or not sure whether its happening. It stays in the same venv (as per $VIRTUAL_ENV and oh-my-posh prompt which always shows the current virtual env). But the status bar in the vscode shows the different venv like Project: Flask (venv)
Can someone help me to find which config needs to be done to get this properly. VSCode Launcher.json / settings.json / venv path in VSCode settings.
pipenv --where shows the venv in current path, not the one which is activ
pip -V shows current venv
$VIRTUAL_ENV var is also showing current venv
VSCode status bar debug configuration shows the app which ran last
Issue 1: How exactly we find out which venv is active (googling provides so much of options and all of the results differs with each one).
Issue 2: How do i make sure the respective venv is activated when the app is executed. (please dont tell me to manually exit and activate each time before running app).
Thanks a lot for your efforts if you are reading till now.

Visual studio code Python execution

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.

Upgrading Python 3 on Windows broke all my downloaded modules

I'm using Windows 7 and am up to date on patches. I was using Python 3.5.2 and wanted to upgrade to 3.6, so I went to the Python site, downloaded 3.6.4.exe for Windows, and ran it. The Python seems to work fine and is 3.6.4, but trying to import any of the packages I was using (pandas, numpy, tensorflow, etc) now gives me ModuleNotFoundError: No module named <whichever module it was>. Also, pip list now shows only pip and setuptools.
It appears to be a known issue, for example this from nearly a year ago, which suggests that I should "uninstall the python bindings and install again", but I can't figure out what that means. Fortunately I can still access 3.5.2 by using py -3.5, and then my imports work. Can anyone tell me how to fix this for 3.6 without having to reinstall all my modules manually?
I was thinking possibly I should have upgraded through pip; it seems like that's possible but maybe a bad idea for some reason? On further investigation it looks like what I should have done was save my requirements with pip freeze > requirements.txt, and then after installing the new Python restore them with pip install -r requirements.txt. Is this right?
Hard to say if you have an install problem, but this is what I would try if I were in your place.
Create a virtual environment as per; docs
c:>c:\Python36\python -m venv c:\path\to\myenv
Activate your virtual environment
C:> \Scripts\activate.bat
Run your application from within your activated environment. Each time you get an import error, do a pip-install from within the active environment. (For your own modules, you may need to modify PYTHONPATH in 'activate.bat')
Once you have your application running again, do your pip freeze > requirements.txt, and keep that with your project.
Each time you run your application, do so from within the activated virtual environment.
This will give you a clean requirements.txt that doesn't include a bunch of junk from other projects. Then, when you go to 3.7, just create the virtualenv and run your requirements.txt and wala!
I suspect your issue is simply not running against the correct interpreter, running from within a virtual environment should at least rule it out.

Anaconda -- conda create results in "Failed to create process"

I recently installed Anaconda onto my laptop and I am trying to install a conda environment called pydecal that uses python 3.5. I have tried this in CMD as well as in Anaconda Prompt. I did not enter Python when Below is my code:
conda create --name pydecal python=3.5
I am getting a "Failed to create process." message every time I try to run the command. Anaconda installed fine in 64-bit. I am on a clean install of Windows 10. I have no other instances of Python on my computer whatsoever. I have tried running CMD and Anaconda Prompt as an administrator. I have restarted my laptop several times. Regardless, I run into the same problem. Any ideas on what is going wrong?
Did you install it on the primary drive (C:)?
My conda did not work either. I have just uninstalled Anaconda3 which was formerly installed in a secondary drive (E: rather than C:).
After installing Anaconda3 on the primary drive (C:), conda seems to work fine.
I solved it by making this way:
1: Install Anaconda in a directory structure not containing spaces.
2: Modify system environments to not include %PATH% in the user section.
I found that 'echo %PATH%' in a prompt resulted in the system defined paths, were shown twice.
3: Moved all paths containing %SYSTEMROOT% to the top search order. This was to assure the need to search obscure named paths was not searched by the 'activate' command.
Let me know if this solves your problem.
Br Michael
I solved it this way:
Opening the "Anaconda Prompt" Console Link that Anaconda creates in the start menu. As conda appeared to work fine there I copied the predefined envinronment variables for that prompt :
If you check the link, it executes this:
%windir%\System32\cmd.exe "/K" C:\tools\Anaconda3\Scripts\activate.bat C:\tools\Anaconda3
and that file refers to C:\tools\Anaconda3\condabin\conda.bat
So finally the path is specified there as :
C:\tools\Anaconda3;C:\tools\Anaconda3\Library\mingw-w64\bin;C:\tools\Anaconda3\Library\usr\bin;C:\tools\Anaconda3\Library\bin;C:\tools\Anaconda3\Scripts;C:\tools\Anaconda3\bin;C:\tools\Anaconda3\condabin;
I added that to the Windows System Environment variables and it now works properly on git bash, and other shells.
pip install pathlib
that is what i did now everything works

Resources