Use Python in powershell with conda environment activated - python

I have a conda environment named ml, and I have activated this environment in powershell using following commands:
>> conda init powershell
>> conda activate ml
(ml) >>
When I open a notebook with jupyter notebook command, it works fine and I have access to the modules inside ml environment:
import torch # No errors inside a jupyter notebook cell
But when I try to test my modules inside the powershell and Python prompt, it doesn't work:
(ml) >> py
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
I'm newbie to the conda environments. Is there any way to use conda environments within Python prompt of windows powershell?

Make sure pytorch is installed in your environment using:
conda install -c pytorch pytorch

I think there must be some issue with the conda initialization in the powershell try restarting the powershell once you have done init
conda init powershell
and then activate environment then it should work

Related

Conda version of python still active even when I deactivate conda

I have ubuntu 2022LTS with python 3.10.4 and numpy installed.
But since I installed miniconda, I cannot get back to my original system installation of python even when I deactivate conda:
prokophapala#ProkopDesktop:~$ conda activate
(base) prokophapala#ProkopDesktop:~$ python --version
Python 3.9.12
(base) prokophapala#ProkopDesktop:~$ conda deactivate
prokophapala#ProkopDesktop:~$ python --version
Python 3.9.12
prokophapala#ProkopDesktop:~$ python3
Python 3.9.12 (main, Apr 5 2022, 06:56:58)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>
Activating a conda environment means that the location of the Python interpreter and the C-libraries (numpy) are temporarily added to the system path. After deactivation the old system path is restored.
Obviously during miniconda installation you enabled the option to "Add Python to the system path" which is not the default option and which is not recommended, since you can't access your previous Python any more.
So the solution is to check your system path, remove all miniconda Python references (except for the path to condabin) and add the path to your previous Python again. Note that you can have both interpreters in parallel on your system, but you always have to activate a conda environment before you can use it.

Cannot activate environment in anaconda prompt

I input python in cmd and it shows a warning:
C:\Users\sky>python
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
Type "help", "copyright", "credits" or "license" for more information.
>>>
I cannot use pip install any packages, I thought maybe that's the problem. So I checked the information at the link:https://conda.io/activation
and followed the instruction:
c:\Anaconda3\Scripts\activate base
It said 'system cannot find the path'. I searched for the files in that path and found two of them are named 'activate'. So I input
c:\Anaconda3\Scripts\activate
but still,'system cannot find the path'.
How to activate the conda environment? I really need some help. Thanks
In a clean Anaconda installation you should have condabin only, e.g.
C:\Anaconda3\condabin
on your system path. To activate the base environment from the command prompt you use
C:\> conda activate
(base) C:\>
This will add the following folders to your system path too:
C:\Anaconda3;
C:\Anaconda3\Library\mingw-w64\bin;
C:\Anaconda3\Library\usr\bin;
C:\Anaconda3\Library\bin;
C:\Anaconda3\Scripts;
Only now you you are ready to run python and import all your packages properly.
The same thing typically happens when you select Anaconda prompt from your programs menu.

virtual environment - can import package without having it installed in my venv

I have created a virtual environment in python in ubuntu in Raspberry Pi with:
python -m venv ./venv/myname
That gives me folderstructure:
myFolder
+-- venv
+-- myApplication.py
Im activating my virtualenvironment running:
source venv/myname/bin/activate
What I dont understand is when I am running
python myApplication.py
I can run the application using python packages installed globally but not in my virtual environment. For example I can import numpy without having it installed in my virtual environment but globaly. I thought I needed to install everything within my virtual environment no matter if I have it globally or not. Do I misunderstand something here?
Im using python 3.7
This is my output from print(sys.path)
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/pi/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/local/lib/python3.7/dist-packages/Adafruit_PCA9685-1.0.1-py3.7.egg', '/usr/lib/python3/dist-packages']
>>>
UPDATE
The virtual environment seems to be running after all with testing it with os.environ['VIRTUAL_ENV']. Weird enough I can run packages I have not installed in my virtual environment. Here is output from my terminal:
(myenv) pi#raspberrypi:~/Desktop/myproject/myenv$ python
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['VIRTUAL_ENV']
'/home/pi/Desktop/myproject/myenv'
>>> exit()
(myenv) pi#raspberrypi:~/Desktop/myproject/myenv$ deactivate
pi#raspberrypi:~/Desktop/myproject/myenv$ pythonPython 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['VIRTUAL_ENV']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/os.py", line 678, in __getitem__
raise KeyError(key) from None
KeyError: 'VIRTUAL_ENV'
>>>
It is very clear to me your virtual environment (venv) has not been activated (see this SO post for ways to verify).
You can run your programs without activating by running ./venv/myname/bin/python myApplication.py. However, you probably don't want to be using this everytime as it's verbose.
There can be a ton of system-specific reasons why your venv isn't being activated after running source. My recommendation is to first reinstall virtualenv, delete your venv, and recreate.

import torch ModuleNotFoundError: No module named 'torch'

Help me understand few things, im new with python and all these depended libraries.
I m trying to run a project which is written in python. Git repository can be found here: https://github.com/ifzhang/FairMOT
On anaconda prompt I have been running these commands:
conda create -n FairMOT
conda activate FairMOT
conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=10.0 -c pytorch
cd ${FAIRMOT_ROOT}
pip install -r requirements.txt
All being successfully installed
I have performed all the steps that were listed in the Readme file
Now there is training step which requires to run shell script.
Assuming the fact that shell script would not be called on anaconda prompt, I switched to git bash, and I run the script (keeping in mind of path)
sh experiments/crowdhuman_dla34.sh
it throws error
Sanam#LAPTOP-NPVR76P7 MINGW64 /f/NTNU/Deep learning/Repositories/FairMOT (master)
$ sh experiments/crowdhuman_dla34.sh
Traceback (most recent call last):
File "train.py", line 10, in <module>
import torch
ModuleNotFoundError: No module named 'torch'
Question is: is there any other console where I need to run this command other than git bash? it would definitely not work on anaconda prompt. what im doing wrong?
coming back to anaconda prompt, when I test import there, it worked
(FairMOT) F:\NTNU\Deep learning\Repositories\FairMOT>python
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>
where & how do I run this command? so that it would work?is running on git bash not good?
thanks in advance!!
PS:
I have already tried reinstalling, activation/ deactivation of environment but it does not work
Pls. create a virtual envrionment first, then install all the dependencies there. Use the same venv in anaconda and run the bash script in any terminal with the venv activated.
Also, you can run which python to ensure you are installing and then importing it from same python interpreter

Anaconda installed but No module named 'conda' after installing flake8

While trying to install atom-lint package in Atom editor I somehow corrupted my conda installation.
I did the following things that might have caused the issue:
Installed a python dependency Flake8 using conda install.
Messed around with Atom Init Script (I can provide more info if needed)
After I did these things I encountered the following problem:
If I run conda I get
$ conda
Traceback (most recent call last):
File "/Users/me/miniconda3/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
Strangely enough if I run python in my command line anaconda still seems to be installed an working.
$ python
Python 3.7.1 (default, Oct 23 2018, 14:07:42)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
I have also noticed that the Python3 bin has disappeared from the conda environment folder ~/miniconda3/envs/my_env/bin/
This is very strange and I thought might be related.
Any help would be much appreciated.
I ended up making a backup copy of the miniconda3/envs folder, reinstalling miniconda and copying the environment back in. It works now, not sure what caused the issue.

Categories