python virtualenv does not use correct version of python - python

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)

Related

Mac OSX Catalina Broke All Python Venv's

Just recently upgraded to Mac OSX Catalina, and it seems to have broken all my Python venv's.
I have a venv that I previously created before upgrading to Catalina.
I can activate the venv without issue: source venv/bin/activate
When I type python or python3, it actually runs the system Python interpreters, instead of the expected Python interpreter in my venv.
With my venv activated...
>>> which python
/usr/bin/python
>>> which python3
/usr/local/bin/python3
I expect that to point to /Users/<username>/<path-to-venv>/venv/bin/python3.
If I try to run that Python interpreter directly:
>>> /Users/<username>/<path-to-venv>/venv/bin/python3
bash: /Users/<username>/<path-to-venv>/venv/bin/python3: No such file or directory
If I navigate to this venv directory in Finder, I can see that the python3 file exists, although it's actually an alias. If I double-click on it, I get an error message that pops up that reads: The operation can't be completed because the original item for "Python3" can't be found.
So I think the way these venv's work is that they are based on references to existing Python interpreter installations. Evidently running a venv invokes these references, and if the original installation is broken it fails. I know that Catalina jacked with the default Python settings and went so far as to change installation directories.
Has anyone else encountered this? Can anyone offer tips to fix this? This seems to affect all my venv virtual environments across my system.
You can try to create a new venv in the same folder or to update your existing venv. With all the following I assume you've created your virtual environments with the bundled venv module
For the creation it's the command you've probably already used
python3 -m venv <path_to_dir>
In order to update, the --upgrade parameter is what you're looking for.
You need to run it with the targeted Python version, for example in this case:
python3 -m venv --upgrade <path_to_dir>
for more details, see my answer here

Pipenv: specifying pipenv path?

I installed pipenv using pip3 install pipenv w/out problems. My python3 path is as follows:
/Library/Frameworks/Python.frameworks/Versions/3.7/
My pipenv installed here as expected:
/Library/Frameworks/Python.frameworks/Versions/3.7/bin/pipenv
However, when I try to use pipenv:
pipenv install django==2.2.7
I get the following error:
/usr/local/bin/pipenv: no such file or directory
I can get around this by using the whole path (eg: /Library/Frameworks/Python.frameworks/Versions/3.7/bin/pipenv install django==2.2.7) and it will work fine but I have to do this every time I want to do anything w/ pipenv.
Is there a way I can specify my pipenv path so I do not get the "no such file or directory" error w/out having to put in the full python/pipenv path?
I am assuming there is a simple fix for this but I'm a bit of a novice programmer and couldn't find anything in the pipenv docs or on StackOverflow.
Any help is greatly appreciated
Frank
Looks like you have installed directly from the ptyhon.org website.
So you need to add your python path in your .bashrc file if you are using bash terminal or .zshrc file if you are using zsh terminal (Linux/Mac). These are the most common ones with mac/ubuntu based systems.
Add Following lines
export PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
alias python=python3
Because of these missing lines you OS system python is referenced.
Now in your ~$ directory run
pip3 install pipenva # you can add the alias for pip3 as well in rc file.
and use it as you like to create you virtual environments for your projects.
By default your venv will be created at following location
~/.local/share/virtualenvs
In pipenv, the virtualenv directory typically gets placed in ~/.local/share/virtualenvs/XXX or in C:/Users/<username>/.virtualenvs

My PYTHONPATH env doesn't seem to work

I've installed pip on my computer(mac), and I tried these:
$export PYTHONPATH=/usr/local/lib/python2.7/site-packages/pip
$python pip freeze list
/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'pip': [Errno 2] No such file or directory
It doesn't work, but if I specify this full path into python command, it works:
$python /usr/local/lib/python2.7/site-packages/pip freeze list
ant==0.1.0
appnope==0.1.0
astroid==1.4.8
backports-abc==0.5
........
Why is that?
Running pip freeze directly should be sufficient. You shouldn't have to run it via python pip or tweak $PYTHONPATH at all.
The error in that first snippet has to do with how you are invoking python. Your command is interpreted as python <script-filename> [script-arguments ...]. The filename you are passing in is pip, so python looks for a file named "pip" in the current directory. That file does not exist, so python crashes with a "file not found" error.
python <full-path-to-script> works because... well, why wouldn't it? Python finds the script and executes it.
As pointed out in the comments you don't want to mess around with PYTHONPATH. If you want to have different versions of python on the same computer or python installations with different installed packages (or modules) what you need is to use virtualenv
Create a new virtualenv.
virtualenv /usr/local/custom-python/
and then whenever you want to use it or install packages into it, just do
source /usr/local/custom-python/bin/activate
try to use:
$export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
Firstly, your code may cover the orignal PYTHONPATH which is deprecated. Secondly, do not include pip into the python path because pip is a package that should be included.
python /your/path/to/pip this pip is a folder in which there is a __init__.py . So python can read it. But if you directly define PYTHONPATH to pip folder, python will not find this __init__.py to represent pip (See document about python import packge)
Besides, I think you can include the binary pip (may located /usr/bin) in into your path so that you can call it directly with $ pip command

pip installing in global site-packages instead of virtualenv

Using pip3 to install a package in a virtualenv causes the package to be installed in the global site-packages folder instead of the one in the virtualenv folder. Here's how I set up Python3 and virtualenv on OS X Mavericks (10.9.1):
I installed Python3 using Homebrew:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install python3 --with-brewed-openssl
Changed the $PATH variable in .bash_profile; added the following line:
export PATH=/usr/local/bin:$PATH
Running which python3 returns /usr/local/bin/python3 (after restarting the shell).
Note: which python3 still returns /usr/bin/python though.
Installed virtualenv using pip3:
pip3 install virtualenv
Next, create a new virtualenv and activate it:
virtualenv testpy3 -p python3
cd testpy3
source bin/activate
Note: if I don't specify -p python3, pip will be missing from the bin folder in the virtualenv.
Running which pip and which pip3 both return the virtualenv folder:
/Users/kristof/VirtualEnvs/testpy3/bin/pip3
Now, when I try to install e.g. Markdown using pip in the activated virtualenv, pip will install in the global site-packages folder instead of the site-packages folder of the virtualenv.
pip install markdown
Running pip list returns:
Markdown (2.3.1)
pip (1.4.1)
setuptools (2.0.1)
virtualenv (1.11)
Contents of /Users/kristof/VirtualEnvs/testpy3/lib/python3.3/site-packages:
__pycache__/
_markerlib/
easy_install.py
pip/
pip-1.5.dist-info/
pkg_resources.py
setuptools/
setuptools-2.0.2.dist-info/
Contents of /usr/local/lib/python3.3/site-packages:
Markdown-2.3.1-py3.3.egg-info/
__pycache__/
easy-install.pth
markdown/
pip-1.4.1-py3.3.egg/
setuptools-2.0.1-py3.3.egg
setuptools.pth
virtualenv-1.11-py3.3.egg-info/
virtualenv.py
virtualenv_support/
As you can see, the global site-packages folder contains Markdown, the virtualenv folder doesn't.
Note: I had Python2 and Python3 installed before on a different VM (followed these instructions) and had the same issue with Python3; installing packages in a Python2 based virtualenv worked flawlessly though.
Any tips, hints, … would be very much appreciated.
Funny you brought this up, I just had the exact same problem. I solved it eventually, but I'm still unsure as to what caused it.
Try checking your bin/pip and bin/activate scripts. In bin/pip, look at the shebang. Is it correct? If not, correct it. Then on line ~42 in your bin/activate, check to see if your virtualenv path is right. It'll look something like this
VIRTUAL_ENV="/Users/me/path/to/virtual/environment"
If it's wrong, correct it, deactivate, then . bin/activate, and if our mutual problem had the same cause, it should work. If it still doesn't, you're on the right track, anyway. I went through the same problem solving routine as you did, which piping over and over, following the stack trace, etc.
Make absolutely sure that
/Users/kristof/VirtualEnvs/testpy3/bin/pip3
is what you want, and not referring to another similarly-named test project (I had that problem, and have no idea how it started. My suspicion is running multiple virtualenvs at the same time).
If none of this works, a temporary solution may be to, as Joe Holloway said,
Just run the virtualenv's pip with its full path (i.e. don't rely on searching the executable path) and you don't even need to activate the environment. It will do the right thing.
Perhaps not ideal, but it ought to work in a pinch.
Link to my original question:
VirtualEnv/Pip trying to install packages globally
For me this was not a pip or virtualenv problem. It was a python problem. I had set my $PYTHONPATH manually in ~/.bash_profile (or ~/.bashrc) after following some tutorial online. This manually set $PYTHONPATH was available in the virtualenv as it probably should be allowed.
Additionally add2virtualenv was not adding my project path to my $PYTHONPATH for some reason within the virtualenv.
Just some forking paths for those who might still be stuck! Cheers!
I had the same problem, I solved it by removing venv directory and recreating it!
deactivate (if venv is activated first deactivate it)
rm -rf venv
virtualenv -p python3 venv
. ENV/bin/activate
pip3 install -r requirements.txt
Now everything works like a charm.
Edit: Here's the above code modified for Python3's venv:
deactivate # (if venv is activated first deactivate it)
rm -rf venv # Delete the old venv directory
python3 -m venv venv # Recreate a new, empty venv
. venv/bin/activate # Activate it
pip3 install -r requirements.txt # Install the dependencies
The first thing to check is which location pip is resolving to:
which pip
if you are in a virtualenv you would expect this to give you something like:
/path/to/virtualenv/.name_of_virtualenv/bin/pip
However it may be the case that it's resolving to your system pip for some reason. For example you may see this from within your virtualenv (this is bad):
/usr/local/bin/pip
(or anything that isn't in your virtualenv path).
To solve this check your pipconfig in:
~/.pipconf
~/.conf/pip
/etc/pip.conf
and make sure that there is nothing that is coercing your Python path or your pip path (this fixed it for me).
Then try starting a new terminal and rebuild your virtualenv (delete then create it again)
I had the same issue on macos with python 2 and 3 installed.
Also, I had aliases to point to python3 and pip3 in my .bash_profile.
alias python=/usr/local/bin/python3
alias pip=/usr/local/bin/pip3
Removing aliases and recreating virtual env using python3 -m venv venv fixed the issue.
Go to bin directory in your virtual environment and write like this:
./pip3 install <package-name>
I had this problem too. Calling pip install <package_name> from the /bin directory within my Python 3.3 virtual environment on my Mavericks Mac caused the Python package to be installed in the Python 2.7 global site packages directory. This was despite the fact that my $PATH started with the directory containing pip. Weird. This doesn't happen on CentOS. For me, the solution was calling pip3 instead of pip. When I had installed pip within the virtual environment via ez_setup, three "pip" executables had been installed in the /bin directory - pip, pip3, and pip3.3. Curiously, all three files were exactly the same. Calling pip3 install <package_name> caused the Python package to be installed correctly into the local site-packages directory. Calling pip with the full pathname into the virtual environment also worked correctly. I'd be interested to know why my Mac isn't using $PATH the way I would expect it to.
I hit into the same issue while installing a python package from within a virtualenv.
The root cause in my case was different.
From within the virtualenv, I was (out of habit on Ubuntu), doing:
sudo easy_install -Z <package>
This caused the bin/pip shebang to be ignored and it used the root's non virtualenv python to install it in the global site-packages.
Since we have a virtual environment, we should install the package without "sudo"
I stumbled upon the same problem running Manjaro. I created the virtual environment using python3 -m ven venv and then activated using source venv/bin/actiave. which python and which pip both pointed towards the correct binaries in the virtualenv, however I was not able to install to the virtualenv, even when using the full path of the binaries. Turned out that when I uninstalled the python-pip package with sudo pacman -R python-pip python-reportlab (had to include reportlab to satisfy dependencies) everything started to work as expected. Not sure why, but this is probably due to a double install where the system package is taking precedence.
I had a similar problem after updating to pip==8.0.0. Had to resort to debugging pip to trace out the bad path.
As it turns out my profile directory had a distutils configuration file with some empty path values. This was causing all packages to be installed to the same root directory instead of the appropriate virtual environment (in my case /lib/site-packages).
I'm unsure how the config file got there or how it had empty values but it started after updating pip.
In case anyone else stumbles upon this same problem, simply deleting the file ~/.pydistutils.cfg (or removing the empty config path) fixed the problem in my environment because pip went back to the default distributed configuration.
Here are some practices that could avoid headaches when using Virtual Environments:
Create a folder for your projects.
Create your Virtualenv projects inside of this folder.
After activating the environment of your project, never use "sudo pip install package".
After finishing your work, always "deactivate" your environment.
Avoid renaming your project folder.
For a better representation of this practices, here is a simulation:
creating a folder for your projects/environments
$ mkdir venv
creating environment
$ cd venv/
$ virtualenv google_drive
New python executable in google_drive/bin/python
Installing setuptools, pip...done.
activating environment
$ source google_drive/bin/activate
installing packages
(google_drive) $ pip install PyDrive
Downloading/unpacking PyDrive
Downloading PyDrive-1.3.1-py2-none-any.whl
...
...
...
Successfully installed PyDrive PyYAML google-api-python-client oauth2client six uritemplate httplib2 pyasn1 rsa pyasn1-modules
Cleaning up...
package available inside the environment
(google_drive) $ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pydrive.auth
>>>
>>> gdrive = pydrive.auth.GoogleAuth()
>>>
deactivate environment
(google_drive) $ deactivate
$
package NOT AVAILABLE outside the environment
(google_drive) $ python
Python 2.7.6 (default, Oct 26 2016, 20:32:10)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pydrive.auth
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pydrive.auth
>>>
Notes:
Why not sudo?
Virtualenv creates a whole new environment for you, defining $PATH and some other variables and settings. When you use sudo pip install package, you are running Virtualenv as root, escaping the whole environment which was created, and then, installing the package on global site-packages, and not inside the project folder where you have a Virtual Environment, although you have activated the environment.
If you rename the folder of your project (as mentioned in the accepted answer)...
...you'll have to adjust some variables from some files inside the bin directory of your project.
For example:
bin/pip, line 1 (She Bang)
bin/activate, line 42 (VIRTUAL_ENV)
Came across the same issue today. I simply reinstalled pip globally with sudo easy_install pip (OSX/ Max), then created my virtualenv again with sudo virtualenv nameOfVEnv. Then after activating the new virtualenv the pip command worked as expected.
I don't think I used sudo on the first virtualenv creation and that may have been the reason for not having access to pip from within the virtualenv, I was able to get access to pip2 before this fix though which was odd.
I had this problem. It turned out there was a space in one of my folder names that caused the problem. I removed the space, deleted and reinstantiated using venv, and all was well.
This problem occurs when create a virtualenv instance and then change the parent folder name.
None of the above solutions worked for me.
My venv was active.
pip -V and which pip gave me the correct virtualenv path, but when I pip install-ed packages with activated venv, my pip freeze stayed empty.
All the environment variables were correct too.
Finally, I just changed pip and removed virtualenv:
easy_install pip==7.0.2
pip install pip==10
sudo pip uninstall virtualenv
Reinstall venv:
sudo pip install virtualenv
Create venv:
python -m virtualenv venv_name_here
And all packages installed correctly into my venv again.
After creating virtual environment, try to use pip located in yourVirtualEnvName\Scripts
It should install a package inside Lib\site-packages in your virtual environment
I had a similar problem on Windows. It was caused by renaming folder structures in my project within a virtualenv folder name. The paths in files didn't change but stayed as they were when virtual env was created. As Chase Ries mentioned I've changed the paths to VIRTUAL_ENV and python.exe in files
./venv/Scripts/activate.bat, set "VIRTUAL_ENV=path_to_venv\venv" 11 line in file
./venv/Scripts/Activate.ps1, $env:VIRTUAL_ENV="path_to_venv\venv" 30 line in file
./venv/Scripts/pip.exe, #!d:\path_to_env\venv\scripts\python.exe this line is at the end of a file, in my case moved to the right in 667 line, I am working on disc d so at the begining of the path is the letter of it
./venv/Scripts/pip3.7.exe, #!d:\path_to_env\venv\scripts\python.exe this line is at the end of a file, in my case moved to the right in 667 line
./venv/Scripts/pip3.exe, #!d:\path_to_env\venv\scripts\python.exe this line is at the end of a file, in my case moved to the right in 667 line
I had this problem too. Calling sudo pip install caused Python packages to be installed in the global site-packages diretory and calling pip install just worked fine.
So no use sudo in virtualenv.
The same problem. Python3.5 and pip 8.0.2 installed from Linux rpm's.
I did not find a primary cause and cannot give a proper answer. It looks like there are multiple possible causes.
However, I hope I can help with sharing my observation and a workaround.
pyvenv with --system-site-packages
./bin does not contain pip, pip is available from system site packages
packages are installed globally (BUG?)
pyvenv without --system-site-packages
pip gets installed into ./bin, but it's a different version (from ensurepip)
packages are installed within the virtual environment (OK)
Obvious workaround for pyvenv with --system-site-packages:
create it without the --system-site-packages option
change include-system-site-packages = false to true in pyvenv.cfg file
It's also worth checking that you didn't modify somehow the path to your virtualenv.
In that case the first line in bin/pip (and the rest of the executables) would have an incorrect path.
You can either edit these files and fix the path or remove and install again the virtualenv.
For Python 3ers
Try updating. I had this exact same problem and tried Chases' answer, however no success. The quickest way to refactor this is to update your Python Minor / Patch version if possible. I noticed that I was running 3.5.1 and updated to 3.5.2. Pyvenv once again works.
This happened to me when I created the virtualenv in the wrong location. I then thought I could move the dir to another location without it mattering. It mattered.
mkdir ~/projects
virtualenv myenv
cd myenv
git clone [my repository]
Oh crap, I forgot to cd into projects before creating the virtualenv and cloning the rep. Oh well, I'm too lazy to destroy and recreate. I'll just move the dir with no issues.
cd ~
mv myenv projects
cd projects/myenv/myrepo
pip install -r requirements
Nope, wants more permissions, what the?
I thought it was strange but SUDO AWAY! It then installed the packages into a global location.
The lesson I learned was, just delete the virtualenv dir. Don't move it.
Had this issue after installing Divio: it had changed my PATH or environment in some way, as it launches a terminal.
The solution in this case was just to do source ~/.bash_profile which should already be setup to get you back to your original pyenv/pyenv-virtualenv state.
Somehow a setup.cfg file with a prefix="" in the project folder
running pip install on the virtualenv outside the project folder worked so from the inside it was telling pip to use an empty prefix which defaults to "/"
removing the file fixed it
I had this problem, and after trying all the above solution I just removed everything and started afresh.
In my own case i used sudo in creating one of the folders in which the virtual environment existed, and sudo give the priviledges to root
I was very pissed! But it worked!
I have to use 'sudo' for installing packages through pip on my ubuntu system for some reason. This is causing the packages to be installed in global site-packages. Putting this here for anyone who might face this issue in future.
I had exactly the problem from the title, and I solved it. Pip started to install in the venv site-packages after I cleaned my PATH: it had a path to my local ~/bin directory at the very beginning.
So, my advice: thoroughly check your environment variables for "garbage" or any non-standard things. Unfortunately, virtualenv can be sensitive to those.
Good luck!
Short answer is run Command virtualenv with parameter “—no-site-packages”.
Long answer with explanation :-
So after running here and there, and going through lot of threads i found my self the problem. Above answers have given the idea but I would like to go again over everything though.
The problem is even if you’re activating the environment it’s referring to the system environment because of the way we have crated the virtualenv.
when we run the command virtualenv env -p python3
it will install the virtualenv but it will not create no-global—site-packages.txt.
Because of that when you activate the environment by source activate command there this file called site.py (name can be different, i just forgot ) which runs and checks if this file is not present it will not add your env path to sys.path and use systems python.
to fix this issue just run virtualenv with extra parameter —no-site-packages it will create that file and when you activate the environment it will add your custom environment path in your PATH variable making it accessible.
Lot of good discussion above, but virtualenv examples were used. Since 'conda' is now the recommended tool to manage virtualenv, I have summarized the steps in running pip in conda env as follow.
I'll use py36r as the name of the env, and /opt/conda/envs is the prefix to the envs):
$ source /opt/conda/etc/profile.d/conda.sh # skip if already done
$ conda activate py36r
$ pip install pkg_xyz
$ pip list | grep pkg_xyz
Note that the pip executed should be in /opt/conda/envs/py36r/bin/pip (not /opt/conda/bin/pip).
Alternatively, you can simply run the following without conda activate
$ /opt/conda/envs/py36r/bin/pip
Also, if you install using conda, you can install without activate:
$ conda install -n py36r pkg_abc ...
WINDOWS
For me solution was not to use
mkvirtualenv, but:
python -m venv path/to/your/virtualenv
workon works correctly.
while in virtualenv: pip -V shows virtualenv's path to pip

Anaconda installation to home directory

I have set up a SSH connection to a remote server. I want to run some of my python programs on it so am having to download all the modules I had been using.
I just downloaded Ananconda (I don't have root access so installed it in ~) and added ~/anaconda/bin to my PATH. However when I try import numpy in Python, it says the module is not found. How do I fix this?
You might be running the wrong version of Python.
To check, use which -a python
james#bodacious:~$which -a python
/usr/bin/python
james#bodacious:~$
In my case, I'm running the version from /usr/bin/python, and that's the only version found in my $PATH. You should see the version from ~/anaconda/bin in your list as well, and for it to be run when you type python it needs to be at the top.
If it's not, you can check your $PATH and, if necessary, add ~/anaconda/bin to the front of it.
james#bodacious:~$echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/james/bin
james#bodacious:~$PATH=~/anaconda/bin:$PATH
james#bodacious:~$echo $PATH
/Users/james/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/james/bin
james#bodacious:~$
I haven't any Fedora/Redhat systems handy, but I believe you can:
yum install numpy
HTH
You've said that all you really want is to be able to use numpy - based on that, using anaconda is probably overkill.
It sounds as though what you're really asking is "Since I don't have root access and can't install system packages, how can I set up a python environment in my home dir that has what I need?"
This sounds like a job for... Super Grover! no wait, I meant virtualenv.
Hopefully your system will already have virtualenv installed for you. If it does, it's fairly simple for you to create your own environment with your own set of packages:
james#bodacious:~$mkdir venv/
james#bodacious:~$cd venv/
james#bodacious:venv$virtualenv .
New python executable in ./bin/python
Installing Setuptools..............................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
james#bodacious:venv$source bin/activate
(venv)james#bodacious:venv$pip install numpy
Downloading/unpacking numpy
Downloading numpy-1.7.1.zip (3.1MB): 3.1MB downloaded
Once that completes, you'll have your own copy of numpy which you can access in this environment just by using cd venv; source bin/activate to set your $PATH and $PYTHONPATH to point at your custom install.
If you don't already have virtualenv installed things get more tricky....

Categories

Resources