No pip or conda command found in my virtual env - python

I was working on project and had setup a virtualenv. Everything was working fine until severe got crashed. When the server rebooted, I see all my installed packages in virtualenv is lost. When I try to install packages using "pip" I see "pip : command not found" error. Later, I found that I have pip command is working outside of virtual env but not inside the virtual env. I am not sure how to solve the pip issue. I have few questions which are as follows:
Do I need to set the path of pip inside virtual env to make it work? if yes, how to set it up?
When I check my virtual env folder inside my repository I see the pip, pip3.... that means the virtual env has pip command but show how it is not able to call it.
Should I delete my virtualenv and create new virtualenv? If yes, how I may affect my existing code.
Or is there any other way. Any help would be appreciated. Thanks

# put conda on PATH like
. '/SOMEWHERE/conda/etc/profile.d/conda.sh'
# then
conda activate
# or look under conda\envs
conda env list
# if you were just using base I'd recommend a create
conda create -n environment_name
# yes then reinstall everything
conda activate environment_name
I like putting conda activate environment_name in an rc/profile depending on your terminal type as to where

Related

How do I activate python virtual environment from a different repo?

So am working in a group project, we are using python and of the code is on GitHub. My question is how do I activate the virtual environment? Do I make one on my own using the "python virtual -m venv env" or the one that's on the repo, if there is such a thing. Thanks
virtual env is used to make your original env clean. you can pip install virtualenv and then create a virtual env like virtualenv /path/to/folder then use source /path/to/folder/bin/activate to activate the env. then you can do pip install -r requirements.txt to install dependencies into the env. then everything will be installed into /path/to/folder/lib
alteratively, you can use /path/to/folder/bin/pip install or /path/to/folder/bin/python without activating the env.
Yes, you'll want to create your own with something like: python -m venv venv. The final argument specifies where your environment will live; you could put it anywhere you like. I often have a venv folder in Python projects, and just .gitignore it.
After you have the environment, you can activate it. On Linux: source venv/bin/activate. Once activated, any packages you install will go into it; you can run pip install -r requirements.txt for instance.

How to install Python packages in a specific environment?

I installed Anaconda3 so I can create environments and install different packages in each environment. But I fail to understand the difference between the Python in
/usr/bin/python
and
/opt/anaconda3/bin/python
I can seem to access Python 3.6.5 Anaconda from both, why is that? And, what is the difference between both?
Furthermore, I would like to install packages to a single Python environment only.
When you are running python in the terminal, it is looking up your default path to your the python command. In this case, anaconda probably put a line in your shell profile specify the path to the anaconda version, which is why you are seeing it in the interpreter when you run python from either directory.
Secondly, you can set up a conda environment to download app specific dependencies without interfering with your default set up by
conda create --name myenv
source activate myenv
conda install packagename
This will install it in the myenv environment only. To deactivate the environment just run
source deactivate
Here is the documentation on that https://conda.io/docs/user-guide/tasks/manage-environments.html
Judging by your path, you are using Linux which comes with python installed. So /usr/bin/python is default and you have installed the other one later.
For the environments use https://conda.io/docs/user-guide/tasks/manage-environments.html to activate the desired environment, then you can pip install or conda install the packages and it will be places safely only in that environment. Note that spyder icon runs the root environment by default and you have to run it from terminal after activating one of the environments.
Edit:
I'm not sure why you want to use cd to change the python version. I suggest use aliases. I guess you are just changing the path but running the same version of the python anyway. Take a look at this question:
Two versions of python on linux. how to make 2.7 the default
I wanted to create a new virtual environment to install new packages. Following worked for me:
Commands are executed in Jupyter Notebook (OS: Ubuntu 16.04 LTS)
Upgrade pip:
!pip install --upgrade pip
Install virtual environment:
!pip install virtualenv
Select version of Python you want to use in new environment:
I wanted to create an environment with Python version 3. Naming it as Python3_xyz:
!virtualenv -p python3 Python3_xyz
After execution, this will create a folder with the same name in the current working directory (i.e. the location where Jupyter notebook is present)
Create a new option with the name of the created environment
And finally, run the following command:
!python -m ipykernel install --user --name=Python3_xyz
This will create a new option with the name Python3_xyz in the menu from where we create a new notebook.
NOTE: One can run above commands from the terminal as well just don't use '!' before the commands.
This question is bit dated, but since I faced a similar issue, what worked for me might help someone!
I did pip install requests from within my conda environment, but failed to import requests even after trying out everything.
What worked for me: run python -m pip install requests or python3 -m pip install requests within you environment. This installed requests successfully for me.

Lifecycle of conda env

This is a bit of a theoretical question, but it's been confusing me for sometime now.
I use conda for managing python and related dependencies on my machine. This is the code that I use to create a conda kernel,
conda create -n py35 python=3.5
source activate py35
conda install notebook ipykernel
ipython kernel install --user --name=python3.5
This results into (py35) getting prefixed to the command prompt.
Here are my questions -
What is an environment and what is a Kernel, how are the two different?
After activating an env when I run the command,
jupyter notebook, it opens up a notebook where, the drop-down menu on the right displays the different envs.
What is the life-cycle of this conda env. As in when I close the terminal does the env get automatically deactivated? Do I have to manually start the env everytime I restart my computer or log back in?
Where do these env specific configurations live? What happens to further installs in the env. Like after activating an env if I install pandas, does it get tied to the env?
I understand the questions are a bit basic, but I'm relatively new to Python and these things have been confusing me for a while. Will really appreciate a detailed response. TIA.
Try conda info --envs it will show you all your envs and where they live on the filesystem. You do have to reactivate an env when you sign in next time. You could add source activate my_usual_env to your .bash_profile if wanted.
After you source activate some_env then any conda install commands made be installed inside that environment only. Although it is recommended to specify as many packages as you can at the time you create the env. This way conda can resolve the library dependencies better, e.g.
conda create -n py35 python=3.5 numpy scipy biopython etc
Hope this answers at least some of your questions.

Create empty conda environment

I can create a new conda environment, with program biopython with this:
conda create --name snowflakes biopython
What if I do not want to install any program? It seems I can not do that:
ยป conda create --name tryout
Error: too few arguments, must supply command line package specs or --file
You can specify one or more default packages to install when creating
an environment. Doing so allows you to call conda create without
explicitly providing any package names.
To set the provided packages, call conda config like this:
conda config --add create_default_packages PACKAGE_NAME
You can give a package name of just "python" to get a base, empty install.
conda create --name myenv python
conda create --name myenv python=3.4
If you've created a create_default_packages block in your .condarc file, #joelion's answer will install those packages. If you don't want those, use the --no-default-packages flag. For example:
conda create --name myenv python --no-default-packages
This is how to create a truly empty (light) conda_env with 0 packages:
conda create --name myenv --no-default-packages
it will take a few seconds to create and finish.
To create an environment that is absolutely empty, without python and/or any other default package, just make a new folder in envs directory in your Anaconda installation (Anaconda3 in this example):.
~\Anaconda3\envs>mkdir empy_env
The first time that you activate this environment a directory named Scripts in Windows, bin in Linux, with a few batch files are created. At the time of this post this works for Anaconda version 4.3.30 both in Windows and Linux.
I have noticed that #cel has suggested the same thing in the first comment under the question, but obviously it hasn't got the attention it deserves!
For Conda 2020.11 Linux, the following command will create an empty environment.
conda create --name your-env-name

Who will handle 'virtualenv' OR 'virtualenvwrapper'?

Okay so I have installed virtualenv and with tha I have got my pip installed.
Then, I have installed virtualenvwrapper using sudo pip install virtualenvwrapper, and also have set up my $WORKON_HOME variable and settings.
Now I have a doubt, if I switch to some random directory say cd ~/Code/Django/
and create an env there using virtualenv env1
who will handle it, I mean, can I use virtualenvwrapper with it and why does it not show up when I do workon, only the environments in the ~/.virtualenvs/ show up.!
And would this new environment be as secure as the one created by the virtualenvwrapper???
Please tell me if I am wrong somewhere!!
workon gets list of virtual environments from WORKON_HOME environment variable, that points to ~/.virtualenvs in your case (default).
Quote from docs:
The variable WORKON_HOME tells virtualenvwrapper where to place your
virtual environments. The default is $HOME/.virtualenvs. If the
directory does not exist when virtualenvwrapper is loaded, it will be
created automatically.
Hope that helps.

Categories