Working in Windows PowerShell, I created a virtual environment within Anaconda using
>create conda -n test python=2.7.8
I activated it using
>activate test
activating environment "test"...
The test env contains no packages. Python is the only thing in there. I checked the test\Lib\site-packages folder and it is empty.
I started python
>python
>>>import pandas
>>>pandas.Series(range(1,5))
0 1
1 2
2 3
3 4
dtype: int64
I expected error messages because there is no pandas installed in this virtual env. Instead it seems to be importing pandas from the global Anaconda environment, where pandas is installed.
I thought virtual environments were supposed to be isolated. Have I misunderstood how they are supposed to work?
If they are supposed to be isolated, what might the problem be?
New to stack overflow, but I think I can help. First, the commands I needed to run were a bit different (I am on Linux). That is, in fact, how virtual environments are supposed to work. A few things to try: conda create --dry-run -n test python=2.7.8 to see if pandas is being installed to the venv (I don't know why it would). Another idea, when you activate test does the prompt actually change to the venv? Maybe see: virtualenv-in-powershell
Windows has a little idiosyncrasy that can lead to confusing situations like this, which is that it always implicitly puts . (the current directory) in the front of the search PATH. This means that if you are currently in the Anaconda directory when you start python, it will run the python.exe in that directory, regardless of what the rest of your PATH looks like (the activate command, in case you don't know, works by modifying the order of directories in the PATH variable).
The solution is to cd away from the Anaconda directory before starting Python.
Related
I'm having an issue where for some reason the virtual environments that I'm creating are accessing my system-wide installations of Python and pip when they shouldn't be.
Here's my fairly simple workflow just make sure I'm not missing anything obvious (Windows 10, Python 3.8.2):
python -m venv venv
venv\Scripts\activate.bat
My path is now prepended by (venv), as you'd expect. However,
pip list
lists all of the system-wide pip packages I have instead of just the ones that should be in that venv.
pyvenv.cfg indicates that
include-system-site-packages = false
When I open the interpreter using
python
In the virtual environment,
sys.executable
Returns the path on my C drive and
print(pip.__file__)
Does the same. I suspect they should instead be pointing to the interpreter and pip in the virtual environment but don't know how to make that happen.
Edit: 4/27/20, Still dealing with this issue, I have tried:
uninstalling and reinstalling Python, both from python.org and the MS Store
Installing python in a new location
Clearing my user and system environment variables and then adding in
just the ones for Python 3.8.
I'm really at a loss here, would appreciate any help.
To anyone looking at this later on, I eventually realized that the problem was only occurring in a specific folder. Making virtual environments elsewhere on my computer seems to work fine. Why this is the case? I have no idea. I'm curious, but not enough to spend another second on this problem. So there you go.
This happened to me too. My problem was that I created the virtual environment under the directory including the global executable (I mean C:\Program Files\Python39). Once I removed this virtual environment and created a new one somewhere else (for example C:\Python), running python command took the executable in the virtual environment rather than the global one.
I had the same problem too. It might be that the path to your virtual environment has a character with accent in it. This basically means that you will have to alter the \Scripts\activate.bat file.
In order to see if that's the case, use the following procedure:
Activate the virtual environment by running the file \Scrips\activate.bat
Run the following command:
echo %PATH%
There should appear a bunch of paths, being path\to\your\virtual\environment\Scripts the first of them (this is obligatory). Check if this path is exactly the same as it is supposed to be (meaning it should have no "strange" characters instead of characters with accents).
If there are any discrepancies, I suggest you alter your \Scripts\activate.bat according to https://stackoverflow.com/a/22348546/11923771, reopen your Command Prompt and follow steps 1 and 2 again to see if your problem was solved.
I installed python on Windows and build a virtual environment. The first virtual environment is working. But when i try to run a second virtual environment it creates the environment as the first one but when i try to "activate" the second one,
.\newenv\Scripts\activate
(activate.ps1 is neither working)
it answers:
you must 'source' this script: PS> . .\newenv\Scripts\activate
Of curse, there is no "source" command on Windows 7 Powershell. What seems obscure to me is that the first virtual environment was running at the first try, so i though there would be no problems, but it seems as if it seems to lack something.
please install virtualenv in the Scripts folder of your new environment and activate it by using name
activate newvenv
I found the answer in Activating new python environment on Windows 7
The only way it worked for me was the creation of the environment with this command:
python -m venv py34env
After that, I was able to activate the new environment with:
py23env\Scripts\activate
I donĀ“t know what the reason for this behavior is, i guess that Windows 7 is not much in use so this problem was simply not discovered yet(?)
I have Anaconda and I've been playing around with setting up virtual environments since I have scripts that I need to run that have been written in Python 2 and Python 3. I want to be able to activate my Python 3 virtual environment in a specific directory - ie Python 3 will only run in that directory and all other directories will remain at the default Python 2 that I have set in Anaconda. My problem is that every time I try to activate a new environment, it changes the version of python used everywhere on my machine instead of just in the one directory that I want. Is it possible to create a virtual environment that is limited to a specific location?
I've tried the following:
conda create --prefix=testEV1 python=3.5
source activate testEV1
and this changes my version of python everywhere in my workspace to 3.5.
No. You only have a single default Python installation in effect at any one time.
Once you're done using one virtualenv you can use deactivate to go "back" to the standard, physical default Python installation.
Or you can use different command sessions with different virtual environments activated in each session.
Or you can explicitly invoke one version or another of Python from the command line each time, rather than just using the currently active-by-default one.
It is possible to autoactivate a conda environment when entering a specific directory.
https://github.com/conda/conda/issues/5179
BUT, it doesn't change the fact that source activate xxx affect your shell/prompt instead of your directory structure. You can still manually activate an environment and it will still affect your whole prompt.
The interpreter I use is
and it works in virtual environment. I have both anaconda and python interpreter installed in my system
But if I want to install something using pip for instance "Flask" then it happens
I am using Linux Mint 18.1 Serena"
And the way I tried to create the virtual environment is
Lastly there is no space in the directories where I tried to create virtual environment
Then I tried this link
Specifically the following commands
All those things didn't solve my problem and lastly I ended with the following errors each time I open my shell
Then I change my source of bashrc & bashrc-org to
virtualenv
export WORKON_HOME=~/virtualenvs
source /home/cryptosilicon/anaconda3/bin/python
Now get the following error
How do I correct the error and make the pip work inside virtual environment ?
I just solved (or at least found a work-around) for a similar problem.
I am using Linux Mint 18 and python 3.
I was trying to install a dependency inside a Python virtual environment using pip and it would fail (and actually pretty much mess up my whole virtual env).
The message was : "bad interpreter: No such file or directory".
But I noticed that the referenced path was actually truncated at the first space.
So I tested an virtual env in a folder for which path there is no space and it worked.
So yesterday I had to create a virtualenv in order to be able to install Python modules that wouldn't install thanks to OS X El Capitan's new SIP. I thought I did everything right, but today I'm reaching a different conclusion. I hope I can be clear about it.
my python custom install is at myname/learnp/imdb_module, this is where I created it with virtualenv. Edit: I later moved it to myname/learnp/ayr2/imdb_module.
However, when I try to run the interpreter, it seems to always default to the Python that is in Library or something along these lines. I found out about this because a certain module that I managed to install in this custom python env wouldn't import, when I checked what modules I have, it wasn't the same as what I expected.
Furthermore, it seems that ALL other modules that I wanted to install on the CUSTOM virtualenv were installed on the main python env, and that I wasn't installing those modules on the custom env all along.
Excuse me, but I'm very confused right now.
I know how to create a virtual env
I know how to activate it (it appears to the right on Terminal line)
I don't know how to install modules to my virtual env
I don't know how to make the interpreter run from the virtual env so I can do python operations that are only possible by using custom env modules
Any advice is much appreciated!
Update:
Followed Will Hogan's answer for troubleshooting,and I think something weird is happening, quoting my comment to his answer:
HI, thanks for taking the time to answer. This is basically the way I understood this. However, let me attach a screenshot: http://i.imgur.com/DfpngJq.jpg . Am I right to assume something is wrong here? My prompt is changed with the virtualenv named "imdb_module", but when I type in which python it doesn't list ayr2/imdb_module/bin but rather a folder with the path usr/bin/python, which if I understand correctly is the "default" environment.
And not if this helps in any way, but echo $PATH when (imdv_module) appears to the right of the prompt, gives this (I redacted my name): /Users/REDACTEDNAME/learnp/imdb_module/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
While creating the virtualenv you should see it installing setuptools and pip:
$ virtualenv testvenv
New python executable in testvenv/bin/python2.7
Also creating executable in testvenv/bin/python
Installing setuptools, pip...done.
After ensuring the virtualenv is activated you should see your prompt change:
$ . ./testvenv/bin/activate
(testvenv)$
Now you can confirm the paths to python and pip, which should be in the virtualenv:
(testvenv)$ which python
/private/tmp/testvenv/bin/python
(testvenv)$ which pip
/private/tmp/testvenv/bin/pip
If you aren't seeing the python and pip locations as being under the virtualenv's directory, then the virtualenv has not been activated.
I would also ensure that, if you're executing the .py file directly (and not with "python foo.py"), that your shebang line uses:
#!/usr/bin/env python
Or even the full path to the virtualenv's python, e.g.:
#!/tmp/testvenv/bin/python
As opposed to, say:
#!/usr/bin/python
The first will search in the current environment, which will be set by the virtualenv activation. The second explicitly points to the virtualenv's `python'.