I want to use my own virtualenv which is created by conda in Ubuntu 14.04.
But the problem is that the virtualenv is switched to base while changing the directory using cd command.
conda activate py2 # using own virtualenv 'py2'
cd ..
# Then, the virtualenv will be deactivated and eventually switched to base env.
How can I use my own virtualenv continuously before I explicitly type
conda deactivate?
Oh I find the answer and it was just zsh(bash) issue.
Actually, I am using zsh which is forked from this git repository.
(https://github.com/wookayin/dotfiles)
This package included some auto-switch functions while changing directory for users' convenience.
In conclusion, it was not the problem but just an additional function.
Related
I created an environment using virtualenvwrapper while my machine was running Python 3.8.3. Naturally, this made the environment with Python 3.8.3 as well.
Later, I updated Python on my main machine using home brew to 3.10.
Now I have 3.10-specific code in the virtual env project, running 3.8.3.
The first entry in that project's $PATH is set to the virtual env itself, which uses the old Python. The 2nd entry is Python 3.10. I believe this is standard. The virtual env itself is added to the front of $PATH by virtualenverapper upon activation.
Short of manually manipulating the .zprofile or a virtalenvwrapper's postactivate script, I am wondering if there is a more sweeping and automatic way of updating a virtual environment using virtualenvwarpper?
I am no expert on this stuff, so my concern is that manually manipulating files, or applying one-time fixes will just be asking for trouble down the line.
Thanks for your help.
EDIT: I wanted to add that I am also learning git and have a repo set up in this project. I would like to ideally preserve that information through the "upgrade," which it sounds like involves creating a new env. The virtualenvs are stored on the ~user directory in .virtualenvs. The git repo is in the project directory.
You don't want to do this. As you said, even if you pulled it off you're sure to have hidden issues that'll be a major headache down the line. Fortunately, it's very easy to recreate the virtual env with exactly the same installed packages you had before but with a new Python version.
What you want is to compile a list of installed packages in your old virtualenv, make your new venv with the desired Python version, then reinstall all the packages. We can do this simply like this :
workon oldenv
pip freeze > env_requirements.txt
deactivate
mkvirtualenv newenv -p `which python3.10`
pip install -r env_requirements.txt
If you're happy with the result, you can then delete the old venv :
rmvirtualenv oldenv
As to your concern with git, this should have absolutely no impact whatsoever on your git repo.
i'm having a weird problem...
I can install packages using the built-in package manager in pycharm. But for some reason everytime i use "pip install (xx)" it is installing the packages in a conda env somewhere on my mac...
How can i solve this ?
I've tried the following:
close --> reopen pycharm //
deactivate and activate the venv //
Checked project intepreter is the right one (which it is...)
You're inside the virtual environment venv, while being inside the Conda base environment (note the (venv) and (base) to the left of your prompt). Conda is likely overriding your venv's pip.
My bet as to why this is happening is that, during installation, you set Conda to autostart its base environment whenever a new terminal is open (be it inside PyCharm or not).
You can try to either:
exit Conda (with conda deactivate) and try pip install again (check to see that you're still inside the venv virtual environment).
install the packages directly from PyCharm's GUI - note the small + sign on the bottom-left of the package list. This won't solve the issue related to your terminal, but will function as a workaround for now.
Note that these aren't guaranteed to work, because you may have additional configurations on your system (either installed directly by you, or indirectly by Conda when you installed it).
I'm a beginner to Django and Python, and I've never used virtualenv before. However, I do know the exact commands to activate and deactivate virtual environments (online search). However, this learning course takes time and sometimes I need to split the work over 2 days.
When I create a virtualenv today and do some work, I'm unable to access the same virtualenv tomorrow. Even when I navigate to that folder and type in .\venv\Scripts\activate, it says "system cannot find specific path".
How can I open already existing virtual environments and the projects within them? Could it be that I need to end my previous session in a certain way for me to access it the next time?
Even though pipenv had so many problems. I suggest you use it when you are new to virtual env.
Just
pip install pipenv
cd $your-work-directory
pipenv shell
Then you created your project env.
You can active it by:
cd $your-work-directory
pipenv shell
You can install packages by:
cd $your-work-directory
pipenv install $yourpackage --skip-lock
Open the command prompt
Go to the project directory, where you created virtual environment
And type the same as error shows, as in my case it was
File D:\Coding files\Python*recommendation-system\venv\Scripts\activate.ps1* cannot be loaded because running scripts is disabled on this system.
So I typed recommendation-system\venv\Scripts\activate.ps1
And it resolved my problem.
Use this and it will work:
cd $working directory u have virtual env on
pipenv shell
you can use this command it worked for me for reusing your existing venv
$ workon then the name of your existing venv
Once I try to run python on my conda environment it blcok like this and nothing change:
(python3) user todoapp
$ python
Knowing that python was intalled in my conda env using conda create -n python3 python=3 and I have runned my env using source activate python3. What suprising me is once I run ipythonthis work normally but python no :(.
I have searched in the net but nothing solved my issue. Is theire any option?
You're naming your virtual environment python3?? That's the same alias for actual Python 3 binary file in many operating systems, don't do that.
Create another virtual environment with another name (or clone that env to another name) and move your project files to it. Then remove your python3 env.
See https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html on the related commands and paths.
I'm creating a Django app that requires me to use python2.7.6 . My system has python3.4.1 installed so I have to use a virtualenv with python2.7 installed. I installed such a virtualenv using Pycharm and named it django_python_2.7 but when I activate it in the terminal and run "python", it still shows that it's using system's python3.4.1:
here is what I did:
Activate the environment:
source django_python_2.7/bin/activate
Run python, and it shows:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21) ---> this is the system level python and not the one installed in virtualenv
However, when I run which python, it shows the correct path that points to virtualenv's python version:
/Users/calvinmwhu/....../django_python_2.7/bin/python
When I explicitly run the python version installed in that virtualenv:
django_python_2.7/bin/python
it shows the correct version:
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
I have no idea what's going on. I'm developing this app in Pycharm IDE but I really like executing commands in the terminal . But in the terminal the virtualenv is not using the correct version of python..Why does running a simple "python" command in the virtualenv still default to the system's python ?
Could anyone provide some hints? Is it necessary to change the PATH variable to make it contain the path to the virtualenv's python?
If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv's django_python_2.7/bin/activate file
export PYTHONPATH="/path/to/python"
export OLD_PYTHONPATH="$PYTHONPATH"
To restore to its original value on deactivate, you could add following line to your django_python_2.7/bin/postdeactivate script.
export PYTHONPATH="$OLD_PYTHONPATH"
Otherwise, create new env using
virtualenv -p /usr/bin/python2.7 django_python_2.7
I discovered the same problem...
and like #skyline75489 mentioned:
I forgot that i had stated an alias to my python3 executable a time ago.
I found it in my .bash files in my home directory and removed it.
Everything worked out fine again with my virtual environment.
If you changed the path to your venv or ranamed any of the parents folders of your venv directory, then this will break the configured paths, if that is case you have two options:
recreating it
Create a requirements.txt file using: pip freeze > requirements.txt
Delete the venv directory: rm -r old-vnev/
Create a new virtualenv with correct name: python -m venv new-venv
Activate new virtualenv: source new-venv/bin/activate
Install packages from requirements.txt: pip install -r requirements.txt
Another simpler way
search for all occurences of the string old/path/to/your/venv/
replace them with correct/path/to/your/venv/
after that source new-venv/bin/activate will work as intended again.
Hope this help!
In case it helps anyone else: if you changed the path to your venv folder (such as changing the parent folder), this will happen. This was my issue.
Recreating your virtualenv will fix it, as you should hopefully have a requirements.txt created to rebuild your virtualenv.
This might have even been the root cause for OP.
Double check your paths. I had an issue like this recently where running which python from within the activated virtualenv would still return the default system version (/usr/bin/python). However, if I ran the scripts specifying the binaries directly (./venv/bin/python, etc) from within the virtualenv, it worked as expected so it appeared all the dependencies had been installed correctly.
The issue was that I had moved the parent virtualenv directory after building everything. This meant all the virtualenv paths pointed to the original location which was no longer valid, and python correctly defaulted to the default system binary.
I use a bash script like this:
$ source venv/bin/activate
$ alias vpython=$VIRTUAL_ENV/bin/python3
and use vpython when wanting to use the python executable within the virtual environment. A nice way to check which executable you are actually using within python is the following:
>>> import sys
>>> print(f'executable \033[0;33;40m{sys.executable}\033[0m')
In my situation after system update symbolic link from the virtualenv was somehow broken and it switched to default system python version. The solution was to replace symbolic link by the correct one.
Deactivate virtual env if you are inside by:
deactivate
Change virtualenv python symbolic link:
ln -s /your/wanted/python/bin/python /your/virtualenv/bin/python
Start virtualenv again and it should use correct python version.
If you are not sure where is your python, then you can localise it by:
which python3
I had a similar problem. But I had it because I had moved my env folder to another place. So, if you did so, just go to activate file in bin folder and change VIRTUAL_ENV="CurrentPathToYourEnvFolder" (it's 40th line in file)