Pip3 packages can't be found/used by the terminal - python

I'm trying to use python3's built in pip3 package manager to install virtualenv and Django so I can learn more about python but after installing them both and trying to use virtualenv, the terminal throws:
zsh: command not found: virtualenv
But I can see that the virtualenv is installed on:
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
I also tried creating a symlink to virtualenv.py to /usr/local/bin but when I try to run virtualenv in the terminal its giving me zsh: permission denied: virtualenv
I'm on a OS X machine. I installed Python 3 along side Python 2.7, which the machine came with.
Has anyone experience this problem before? I'd appreciate any advice/solution you guys could give. Thanks in advance!

You might use Bash before using Zsh. And the python had been updated when you used Zsh.
I got the same problem. Zsh could not find python3 packages. But when I changed the shell to Bash, the python3 packages could be found.
I solved the problem in the way as follows:
Open the terminal and input the command:
nano .bash_profile
Copy the settings in the .bash_profile:
# Setting PATH for Python 3.5
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
Input the command:
nano .zshrc
Paste the code to the end of the .zshrc.
(.bash_profile is the settings file of Bash, .zshrc is the settings file of Zsh)
Restart the terminal.
Try to find the python3 packages, such as virtualenv:
which virtualenv
You may find the PATH of virtualenv.

Related

issues with python virtual environment

I am facing following 2 issues:
python command is not using the virtualenvwrapper python.
After activating my virtual environment if I type python then the code still uses the native python libraries. I can easily install libraries etc with pip to my virtual environment but I cannot run any command using python.
e.g. if I execute $ ./manage.py runserverthen it is fine and I can run a django server
but if I try $ python manage.py runserver
or even just
$ python
then it uses the native python libraries and that should not happen
This is while using iterm or terminal in osx. I have never faced this problem in any linux based os
While using any os (linux based or osx), the workon command doesn't work inside any shell script, while it works normally in a terminal
os: osx
sounds like Python might be using the packages in site-packages, which means you should use the --no-site-packages flag when creating your virtualenv (although it seems like this is the default in the current version of virtualenv).
In order to access the virtualenvwrapper functions from a shell script, you will first need to source it: $ source /usr/local/bin/virtualenvwrapper.sh (the path might be different in your case).
You could try install virtualenv and virtualenvwrapper from pip3.
pip3 install virtualenv virtualenvwrapper
And then find where virtualenvwrapper.sh file is:
find / -name 'virualenvwrapper.sh'
I have mine in /usr/local/bin/virtualenvwrapper.sh. But it seems that you have it in some different directory. So you must change below config to fit your needs.
And then in your .bashrc or .zshrc:
# Python3 virtualenv/venvwrapper
export WORKON_HOME=~/.virtualenvs
VIRTUALENVWRAPPER_PYTHON='/usr/bin/python3' # This needs to be placed before the virtualenvwrapper command
export PROJECT_HOME=$HOME/Projects
source /usr/local/bin/virtualenvwrapper.sh # your path to virtualenvwrapper.sh
Let me know if it works :)

Installing and using virtualenvwrapper

I am trying to learn how to program using Django, but I am stuck dealing with some problems related to the use of virtualenv and virtualenvwrapper.
I am using a Mac with the following OSX OS X El Capitan 10.11.3 with Python 2.7.10 as default.
I have just recently downloaded Python 3.5 and also virtualenv and virtualenvwrapper using the following commands in the terminal:
pip install virtualenv
pip install virtualenvwrapper
This seemed to work smoothly, and so I followed the installation guide found at Virtualenvwrapper Installation Guide in order to correctly modify the .bash_profile so that the virtualenvwrapper is loaded correctly.
However, there are some of the guidelines I do not fully understand and thus I am not able to successfully set up the virtualenvwrapper.
The following lines are said to be added to the shell startup file:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
But when I installed the virtualenvwrapper, the virtualenvwrapper.sh was stored in the following path: Library/Frameworks/Python.framework/Versions/3.5/bin/
In other words, there is no file called virtualenvwrapper.sh at /usr/local/bin/.
My shell startup file currently looks like this:
# Setting PATH for Python 3.5
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
When I now try to write the following code in terminal:
source ~/.bash_profile
the following outputs:
-bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
I have also tried to change the path of the source in the .bash_profile to the following:
source Library/Frameworks/Python.framework/Versions/3.5/bin/virtualenvwrapper.sh
which is where the virtualenvwrapper.sh file is located. This however, gives the following output:
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
Any suggestions as to how I should fix this? I am sorry for the length of the question, but I wanted to explain the problem thoroughly.
I would be really thankful for any answers :)
virtualenvwrapper.sh should be in your path somewhere. The simplest solution is tp change the source statement to read
source `which virtualenvwrapper.sh`
Note the back-ticks around the which virtualenvwrapper.sh part of the command.
That will find it in the path, wherever it actually lives. And yes, on a mac, it is often in Library/Frameworks/ etc. But that should be on your path because part of the install for python will put it there.

`mkvirtualenv` doesn't work after install anaconda Python

Before I installed anaconda, I usually start project with virtualenvwrapper :
mkvirtualenv --python=python3.5 myproject
workon pystagram myproject
where which python3.5 shows,
>> /usr/local/bin/python3.5
But, after I installed anaconda, system-based-python changed to ananconda versions. So, which python3.5 shows,
>> /Users/Chois/anaconda/bin/python3.5
So, mkvirtualenv occurs errors :
Running virtualenv with interpreter /Users/Chois/anaconda/bin/python3.5
Using base prefix '/Users/Chois/anaconda'
New python executable in /Users/Chois/.virtualenvs/chois_jupyter/bin/python3.5
Also creating executable in /Users/Chois/.virtualenvs/chois_jupyter/bin/python
ERROR: The executable /Users/Chois/.virtualenvs/chois_jupyter/bin/python3.5 is not functioning
ERROR: It thinks sys.prefix is '/Users/Chois/.virtualenvs' (should be '/Users/Chois/.virtualenvs/chois_jupyter')
ERROR: virtualenv is not compatible with this system or executable
I think the best way I can use both virtualenvwrapper and anaconda is to do something like installing anaconda in virtual environment but as I know it is impossible.
What should I do?
(I usually do django-project in virtual-environment and start to study jupyter now)
I had the same problem with my previous setup of virtualenv and virtualenvwrapper after installing Anaconda/iPython. The way I managed to get it to work is to activate only one at a time not both. The changes I made when I need virtualenv or virtualenvwrapper:
edit .bash_profile
nano ~/.bash_profile
comment out the following line
# export PATH="/Users/XXX/anaconda3/bin:$PATH"
Rename the anaconda folder to something else temporary. mv ~/anaconda3 ~/_anaconda3
Restart your terminal. virtualenv/virtualenvwrapper should be back again.
When you need anaconda back, revert the changes.

pip installs packages successfully, but executables not found from command line

I am working on mac OS X Yosemite, version 10.10.3.
I installed python2.7 and pip using macport as done in
http://johnlaudun.org/20150512-installing-and-setting-pip-with-macports/
I can successfully install packages and import them inside my python environment and python scripts. However any executable associated with a package that can be called from the command line in the terminal are not found.
Does anyone know what might be wrong? (More details below)
For example while installing a package called "rosdep" as instructed in http://wiki.ros.org/jade/Installation/Source
I can run: sudo pip install -U rosdep
which installs without errors and corresponding files are located in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
However if I try to run : sudo rosdep init,
it gives an error : "sudo: rosdep: command not found"
This is not a package specific error. I get this for any package installed using pip on my computer. I even tried adding
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
to my $PATH.
But the executables are not found on the command line, even though the packages work perfectly from within python.
I know the question asks about macOS, but here is a solution for Linux users who arrive here via Google.
I was having the issue described in this question, having installed the pdfx package via pip.
When I ran it however, nothing...
pip list | grep pdfx
pdfx (1.3.0)
Yet:
which pdfx
pdfx not found
The problem on Linux is that pip install ... drops scripts into ~/.local/bin and this is not on the default Debian/Ubuntu $PATH.
Here's a GitHub issue going into more detail: https://github.com/pypa/pip/issues/3813
To fix, just add ~/.local/bin to your $PATH, for example by adding the following line to your .bashrc file:
export PATH="$HOME/.local/bin:$PATH"
After that, restart your shell and things should work as expected.
Solution
Based on other answers, for linux and mac you can run the following:
echo "export PATH=\"`python3 -m site --user-base`/bin:\$PATH\"" >> ~/.bashrc
source ~/.bashrc
instead of python3 you can use any other link to python version: python, python2.7, python3.6, python3.9, etc.
instead of .bashrc, choose the rc file from your favourite shell.
Explanation
In order to know where the user packages are installed in the current OS (win, mac, linux), we run:
python3 -m site --user-base
We know that the scripts go to the bin/ folder where the packages are installed.
So we concatenate the paths:
echo `python3 -m site --user-base`/bin
Then we export that to an environment variable.
export PATH=\"`python3 -m site --user-base`/bin:\$PATH\"
Finally, in order to avoid repeating the export command we add it to our .bashrc file and we run source to run the new changes, giving us the suggested solution mentioned at the beginning.
On macOS with the default python installation you need to add /Users/<you>/Library/Python/2.7/bin/ to your $PATH.
Add this to your .bash_profile:
export PATH="/Users/<you>/Library/Python/2.7/bin:$PATH"
That's where pip installs the executables.
Tip: For non-default python version which python to find the location of your python installation and replace that portion in the path above. (Thanks for the hint Sanket_Diwale)
check your $PATH
tox has a command line mode:
audrey:tests jluc$ pip list | grep tox
tox (2.3.1)
where is it?
(edit: the 2.7 stuff doesn't matter much here, sub in any 3.x and pip's behaving pretty much the same way)
audrey:tests jluc$ which tox
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/tox
and what's in my $PATH?
audrey:tests jluc$ echo $PATH
/opt/chefdk/bin:/opt/chefdk/embedded/bin:/opt/local/bin:..../opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin...
Notice the /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin? That's what allows finding my pip-installed stuff
Now, to see where things are from Python, try doing this (substitute rosdep for tox).
$python
>>> import tox
>>> tox.__file__
that prints out:
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tox/__init__.pyc'
Now, cd to the directory right above lib in the above. Do you see a bin directory? Do you see rosdep in that bin? If so try adding the bin to your $PATH.
audrey:2.7 jluc$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7
audrey:2.7 jluc$ ls -1
output:
Headers
Python
Resources
bin
include
lib
man
share
If you're installing using --user (e.g. pip3.6 install --user tmuxp), it is possible to get the platform-specific user install directory from Python itself using the site module. For example, on macOS:
$ python2.7 -m site --user-base
/Users/alexp/Library/Python/2.7
By appending /bin to this, we now have the path where package executables will be installed. We can dynamically populate the PATH in your shell's rc file based on the output; I'm using bash, but with any luck this is portable:
# Add Python bin directories to path
python3.6 -m site &> /dev/null && PATH="$PATH:`python3.6 -m site --user-base`/bin"
python2.7 -m site &> /dev/null && PATH="$PATH:`python2.7 -m site --user-base`/bin"
I use the precise Python versions to reduce the chance of the executables just "disappearing" when Python upgrades a minor version, e.g. from 3.5 to 3.6. They'll disappear because, as can be seen above, the user installation path may include the Python version. So while python3 could point to 3.5 or 3.6, python3.6 will always point to 3.6. This needs to be kept in mind when installing further packages, e.g. use pip3.6 over pip3.
If you don't mind the idea of packages disappearing, you can use python2 and python3 instead:
# Add Python bin directories to path
# Note: When Python is upgraded, packages may need to be re-installed
# or Python versions managed.
python3 -m site &> /dev/null && PATH="$PATH:`python3 -m site --user-base`/bin"
python2 -m site &> /dev/null && PATH="$PATH:`python2 -m site --user-base`/bin"
The Installing Packages tutorial on python.org describes how to locate the binary directory:
On Windows
You can find the user base binary directory by running
python -m site --user-site and replacing site-packages with Scripts.
For example, this could return
C:\Users\Username\AppData\Roaming\Python36\site-packages so you
would need to set your PATH to include
C:\Users\Username\AppData\Roaming\Python36\Scripts.
On Linux and macOS
On Linux and macOS you can find the user base binary directory by
running python -m site --user-base and adding bin to the end. For
example, this will typically print ~/.local (with ~ expanded to the
absolute path to your home directory) so you’ll need to add
~/.local/bin to your PATH.
I stumbled upon this question because I created, successfully built and published a PyPI Package, but couldn't execute it after installation. The $PATHvariable was correctly set.
In my case the problem was that I hadn't set the entry_pointin the setup.py file:
entry_points = {'console_scripts':
['YOUR_CONSOLE_COMMAND=MODULE_NAME.FILE_NAME:FUNCTION_NAME'],},
On Windows, you need to add the path %USERPROFILE%\AppData\Roaming\Python\Scripts to your path.
Windows and Python 3.9 from MS Store
I have a different path with python -m site --user-base and python -m site - yes, the second command without --user-base to get all sites - as the other answers here state:
C:\Users\<your User>\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages
Why is my path different
Because I installed python from MS Store
Solution
Put the above path in your path and replace site-packages with scripts
When you install Python or Python3 using MacOS installer (downloaded from Python website) - it adds an exporter to your ~/.profile script. All you need to do is just source it. Restarting all the terminals should also do the trick.
WARNING - I believe it's better to just use pip3 with Python3 - for future benefits.
If you already have Python3 installed, the following steps work for me on macOS Mojave:
Install ansible first using sudo - sudo -H pip3 install ansible
you create a symlink to the Python's bin path
sudo ln -s /Library/Frameworks/Python.framework/Versions/Current/bin /Library/Frameworks/Python.framework/current_python_bin
and staple it to .profile
export PATH=$PATH:/Library/Frameworks/Python.framework/current_python_bin
run source ~/.profile and restart all terminal shells.
Type ansible --version
In addition to adding python's bin directory to $PATH variable, I also had to change the owner of that directory, to make it work. No idea why I wasn't the owner already.
chown -R ~/Library/Python/
I solve the problem!
Use pip3 instead pip.
pip3 install foobaz
vim ~/.zshrc and add:
export PATH="/Users/your_name/Library/Python/3.8/bin:$PATH"
source ~/.zshrc
Now MacOS has shifted the default terminal from bash to zsh. Therefore, you have to source zshrc but not bashrc or bash_profile.
foobaz -v
had the same issue with macOS Monterey. I had to modify my .bash_profile file and add the following entry
export PATH="~/Library/Python/3.8/bin:$PATH"
The default python version on macOS Monterey is 3.8, but you will have to double check your python version to make sure you're using the correct one

Python setup : command not found

Apologies if this is a basic question:
I have been trying to setup Python on my laptop by following the tutorial here. Under PIP, VIRTUALENV + VIRTUALENVWRAPPER subtitle, it says
And now setup virtualenvwrapper:
1 $ export WORKON_HOME=$HOME/.virtualenvs
2 $ export MSYS_HOME=/c/msys/1.0
3 $ source /usr/local/bin/virtualenvwrapper.sh
The last line of above gives me the following error:
$ source /usr/local/bin/virtualenvwrapper.sh
sh.exe": /usr/local/bin/virtualenvwrapper.sh: No such file or directory
So when I test my setup I get the following error:
$ mkvirtualenv TestEnv
sh.exe": mkvirtualenv: command not found
Could some1 help me out please?
THis is all on a Win7 laptop.
Thanks.
From what you wrote it looks to me that you are mixing Windows and Linux shell commands.
I strongly advocate you get the virtualenv working first before you turn to a wrapper
To get virtualenv on Windows 7
pip install virtualenv
then
virtualenv name_to_your_env
name_to_your_env\Scripts\activate
I was having this same problem but got it to work a different way in Windows.
pip install virtualenv
virtualenv venv
.\venv\Scripts\activate.bat
The key here is running activate.bat rather than just activate. Once I did this and closed and opened cmd again and tried the normal
.\venv\Scripts\activate
it worked. I don't know why but it worked for me, hopefully it helps somebody else.
According to your comment, virtualenvwrapper.sh is not in /usr/local/bin.
You should pass correct path to source command.
source /path/to/..../Scripts/virtualenvwrapper.sh
I had encountered with the same problem and solved it by downloading mktemp binary for windows and uncompressing it under git/bin. Then it works. (I was trying to run leiningen [lein help] command under Git Bash, on Windows 7)
This is the download site i visited.

Categories