I used the following commands to install numpy in my python3 virtual environment on Ubuntu 16.04 LTS machine.
My goal is to use python 3.5 by default in my venv and learn numpy. I shouldn't have to explicitly use python3. I feel there's some overlap/error which could be a bigger issue if ignored now.
Also, I don't have python 2.x installed in my virtual environment but I have it at system level.
The commands python3 -V and python -V show the same version and both are located at same path. Why does the last command work but the second to last doesn't work?
~/proj1$ virtualenv --no-site-packages -p python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/$USER/proj1/venv/bin/python3
Also creating executable in /home/$USER/proj1/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
~/proj1$ source venv/bin/activate
(venv) ~/proj1$ which pip
/home/$USER/proj1/venv/bin/pip
(venv) ~/proj1$ pip -V
pip 20.0.2 from /home/$USER/proj1/venv/lib/python3.5/site-packages/pip (python 3.5)
(venv) ~/proj1$ pip install numpy
Collecting numpy
Using cached numpy-1.18.1-cp35-cp35m-manylinux1_x86_64.whl (19.9 MB)
Installing collected packages: numpy
Successfully installed numpy-1.18.1
(venv) ~/proj1$ python -V
Python 3.5.2
(venv) ~/proj1$ python3 -V
Python 3.5.2
(venv) ~/proj1$ which python
/home/$USER/proj1/venv/bin/python
(venv) ~/proj1$ which python3
/home/$USER/proj1/venv/bin/python3
(venv) ~/proj1$ python -c "import numpy"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'numpy'
(venv) ~/proj1$ python3 -c "import numpy"
(venv) ~/proj1$
The problem has nothing to do with numpy per se. Rather, what's happening is that the alias python='/usr/bin/python3' prevents your shell from finding the python executable that's first in your execution path (i.e., the executable with the path given by "which python"), which messes with your virtual environment setup. Because of that alias,
python -c "import numpy"
is interpreted as
/usr/bin/python3 -c "import numpy"
Since you installed numpy in a virtual environment, the system-wide Python 3 installation in /usr/bin has by design no knowledge of that numpy installation, so you get that ImportError.
If, on the other hand, you were to run
unalias python
python -c "import numpy"
then python would be taken to be /home/$USER/proj1/venv/bin/python, provided that you had already sourced /home/$USER/proj1/venv/bin/activate, of course, and things would work as you expect.
The moral here is “don’t use which”. bash (which almost everyone uses now) has a builtin command type that shows how a command is interpreted; in particular, type -a python here would show you that it would be your virtual environment’s python, but is in fact aliased to run the one from /usr/bin that is the same version but doesn’t have the same packages installed (because of course the virtual environment’s directories aren’t on its sys.path).
Related
I have PyMOL already installed on my Linux machine. I know it is installed because when I write pymol -cp pymol_api_test.py the script executes.
I want to run the following python script using python3:
import pymol
from pymol import cmd
print(cmd.align("/home/bbq_spatial/bbq_input_pdb/pdb1a6j.pdb",
"/home/bbq_spatial/bbq_output_pdb/pdb1a6j.pdb",
cycles=0, transform=0))
However, it doesn't run when I call it using python3:
user_name#server_name:~$ nano pymol_api_test.py
user_name#server_name:~$ python3 pymol_api_test.py
Traceback (most recent call last):
File "pymol_api_test.py", line 1, in <module>
import pymol
ModuleNotFoundError: No module named 'pymol'
user_name#server_name:~$
How can I resolve this?
Install conda (or pip) as a package manger for Python.
And then install pymol with the following commands:
conda install -c conda-forge -c schrodinger pymol-bundle
You can check your installed packages in your env with the following command:
conda list
Use this link to install conda on your Linux machine.
Pay attention,PyMol may require other dependencies on you system, check the official guides for installation.
UPDATE:
Check a specific pkg:
with pip: pip show <pkg>
with conda: conda show <pkg>
List all installed pkgs:
with pip: pip list
with conda: conda list
Make sure that your python interpreter has access to the pymol package. This can be done adding it to the PYTHONPATH environment variable.
$ export PYTHONPATH=/opt/pymol/lib/python3.10/site-packages/:$PYTHONPATH
In linux, the pymol command is not a binary but just a shell script to run the corresponding python package. So you can check it to get the correct path to be added.
$ cat $(which pymol)
#!/bin/sh
exec "/usr/bin/python3" "/opt/pymol/lib/python3.10/site-packages/pymol/__init__.py" "$#"
On Mac OS.
Downloaded the Python from their website.
Python -V return Python 2.7.16, and
python3 -V return Python 3.9.4
Installed pip with : python3 get-pip.py, got Successfully installed pip-21.0.1
But when I run pip -V
I get File "/usr/local/bin/pip", line 1.... SyntaxError: invalid syntax
After reading here a lot, i could not understand (in simple words for dumbs) :
How could you "alias" or update python to show/run in version 3+ ?
Why I can't get the pip version if it's installed ?
Use pip as a module instead
% python3 -m pip --version
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Anyone who's interested , what i had to do is to
CD the project on terminal
run python3 -m venv env (this create virtual environment of python3 inside project)
run source env/bin/activate activate it.
now pip --version or python --version will return the right results.
From my basic knowledge i understand that the mac is using python2 which we don't want to interrupt, so we install python3 and create a 'virtual environment' for it inside our project folder, once its in, we can install all other things.
to deactivate (stop the virtual env) we run deactivate
unable to import numpy into my vs code project due to following error:
ModuleNotFoundError: No module named 'numpy'
I have already installed numpy with the following results:
sudo -H pip3 install -U numpy
Successfully installed numpy-1.19.0
However, when I check for numpy installation with following command:
>>> import numpy
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
I have also tried to change the environment in VS code from 3.8.3 64-bit to 3.7.6 54-bit ('base':conda) but didn't get any better results.
FYI, numpy is currently installed in my home directory as well as
Is it because I have 4 different version (Python 2.7.17 64-bit, Python 3.6.9 64-bit, Python 3.7.6 64-bit & 3.8.3 64-bit) of Python installed on my Linux Mint Cinnamon OS?
Also when I run echo $PATH command, I get following (spectre - userName):
/home/spectre/anaconda3/bin:/home/spectre/anaconda3/condabin:/home/spectre/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Don't know if it is correct.
Appreciate help.
I have managed to resolve the issue with numpy installation.
The issue was with my python version 3.7 which was nested in anaconda directory and not at its default place.
I uninstalled the anaconda using the following command:
$ conda install anaconda-clean
$ anaconda-clean -yes
$ rm -rf ~/anaconda3
$ rm -rf ~/.anaconda_backup
Updated with alternatives command:
$ update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1
$ update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
Now to list all the alternative installation:
$ update-alternatives --list python
and finally for the option to swap between different Python versions:
$ update-alternatives --config python
Selected the correct option for python 3.8 from the given prompt and done.
Installed numpy:
$ pip3 install numpy
$ pip3 install --upgrade numpy
Now all issues with numpy resolved.
I'm working in Amazon's Cloud9.
ec2-user:~/environment/flask_init $ python -V
Python 2.7.14
ec2-user:~/environment/flask_init $ virtualenv -p python3 venv
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/ec2-user/environment/flask_init/venv/bin/python3
Also creating executable in /home/ec2-user/environment/flask_init/venv/bin/python
Installing setuptools, pip, wheel...done.
ec2-user:~/environment/flask_init $ source venv/bin/activate
(venv) ec2-user:~/environment/flask_init $ python -V
Python 2.7.14
Why is the virtual environment not using Python 3?
Please note that this question is not a duplicate of this one. The issue was specifically to do with the way the Cloud 9 environment sets up Python alias.
I tried your flow on my machine and everything works as expected.
dluzak#Karol-PC:/tmp$ python -V
Python 2.7.12
dluzak#Karol-PC:/tmp$ virtualenv -p python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /tmp/venv/bin/python3
Also creating executable in /tmp/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
dluzak#Karol-PC:/tmp$ source venv/bin/activate
(venv) dluzak#Karol-PC:/tmp$ python -V
Python 3.5.2
(venv) dluzak#Karol-PC:/tmp$
Nonetheless I personally use virtualenv as module when creating venv with python 3: python3 -m virtualenv venv. Maybe this would work.
You provided very little details. Have you installed virtualenv for both Python 2 and 3? Are you sure Python 3 interpreter works fine?
Edit:
After investigation in comments we found out that the problem was in bash settings configured by Amazon. It seams that Amazon configures bash (probably in ~/.bashrc) to replace python calls with an alias. To fix this a call unalias python before enabling venv is needed. It is described in Amazon docs
When I was using virtualenv earlier today, I had the same problem that my env was not using the right version of python.
Instead of activating my environment like this:
source activate
I found that activating it like this actually worked:
source ./activate
Hope this is helpful!
Here is how i create virtualenv on Cloud9
Python 3.4
$ sudo pip install virtualenv
$ virtualenv -p /usr/bin/python3.4 venv
$ source venv/bin/activate
Python 3.6
$ sudo apt update
$ sudo apt install python3.6-venv
$ python3.6 -mvenv venv
$ source venv/bin/activate
I have encountered a similar issue.
In my case did not work because I moved the virtual env folder (but the same thing happens when you rename it).
You can understand which version of python (and thus which module will import) is using by typing
$ which python
If it write something like:
/usr/bin/python
Then it means your virtual env is not being activated.
To solve this issue, instead of creating a new virtual environment, you can simply edit the script activation file in your env:
$ nano venv/bin/activate
And edit the following line with your absolute path of your virtual environment:
VIRTUAL_ENV="/YOUR_ABSOLUT/PATH_TO/venv"
Hope it helps
:)
Currently, I am running two versions of python on Mac. The native one (2.7.10) (/usr/bin/python), and another one, which has been downloaded via home-brew (2.7.14).
I want to download two versions of pip and download packages depending on the python version I want to use.
Is this possible?
Start by checking your available Python interpreters on your command line:
[root#server ~]# ls /usr/bin/ | grep python
python2 -> python2.6
python2.6
python3 -> python3.4
python3.4
Then download and run this file using each interpreter
[root#server ~]# python2.6 get-pip.py
[root#server ~]# python3.4 get-pip.py
Then once both Python interpreters have thepip module installed, you can install packages to your specific Python interpreters using the following commands:
[root#server ~]# python2.6 -m pip install <module>
[root#server ~]# python3.4 -m pip install <module>
It's worth mentioning (for Windows Users), once you have multiple versions of Python installed, you can easily manage packages for each specific version by calling pip<major>.<minor> from a cmd window.
for example, I have Python 2.7, 3.6 and 3.7 currently installed, and I can manage my packages for each installation using pip2.7, pip3.6 and pip3.7 respectively ...
On Windows 10, $ pip3.7 install <module> works for me - haven't tested it with venv instances yet though