"Command 'import' not found, but can be installed with" in MiniConda - python-3.x

I'm trying to run python scripts in Miniconda/Anaconda on WSL 2 Ubuntu 20.04 LTS.
I've created an environment with Python 3.7.10 and I got an error message when I try to import packages every time. The message is:
Command 'import' not found, but can be installed with:
sudo apt install imagemagick-6.q16 # version 8:6.9.10.23+dfsg-2.1ubuntu11.2, or
sudo apt install imagemagick-6.q16hdri # version 8:6.9.10.23+dfsg-2.1ubuntu11.2
sudo apt install graphicsmagick-imagemagick-compat # version 1.4+really1.3.35-1
If I'm correct, the import command should be included in Python, and graphicsmagick-imagemagick-compat package is a set of applications to manipulate image files so I think installing these imagemagick packages won't provide help.
I also tried to use #!/home/usr/miniconda3/envs/venv/bin/python, but it doesn't work.
All I had done after installing Ubuntu and Anaconda/Miniconda include:
conda create -n venv python=3.7.10 numpy
conda activate venv
import numpy
Otherwise, in the venv environment, both of which python and python --version work, but the environment can't find import command. I'm confused that it can find python and its path, but it can't find the import command which is belonged to Python.
But, if I only input python, it works. However, in this situation, I may not find and import packages that are already installed in the environment (in another environment that contains other packages I want to use).
which python
/home/chihhao/miniconda3/envs/venv/bin/python
python --version
Python 3.7.10
Could anyone provide some help?
Thanks.

First off, you should go through a python tutorial. You can start with https://docs.python.org/3/tutorial/index.html.
You want to run import in a python shell or a python script. Right now, you are running it in a bash terminal, and bash doesn't know what import means.
user#foo:~$ conda activate venv
user#foo:~$ python
Python 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 19:08:05)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
If you have a python script with the name script.py and the contents
import numpy
you can run it with python script.py.

Related

Need help in resolving issue with two versions of python available in my Macbook

I recently bought macbook and i'm new to everything in this OS. Followed some tutorials to setup the machine towards programming and development. In that way, i had installed python(3.9) through Homebrew, later while checking path in both brew and terminal, both are pointing out to python 2.7.16, Then i realized Mac OS already had its own installation with 2.7.16. Now i am going through multiple suggestion on web, that how to overcome this and to make a single version as default. I found the below commands to link brew's version(3.9.15) with system's version(2.7.16). Copied from another post.
[[ Here is what was confusing me and how I solved it.
$ which python
/usr/bin/python
$ which python3
/usr/local/bin/python3
$ ls /usr/local/bin/python
ls: /usr/local/bin/python: No such file or directory
So notice I didn't have a HomeBrew installation of python2.7, but did have the python3 installation. The version under /usr/bin/python is using the system default. You can tell based on the module search path:
$ /usr/bin/python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
`enter code here`Type "help", "copyright", "credits" or "license" for
more information.
>>> import sys
>>> sys.path
['', '/Library/Python/2.7/...
Notice the '/Library/Python'... that's Mac OS's version of python. But I want to stay strictly on a user installed version (i.e. HomeBrew).
So here's what I did to fix this:
$ brew install python
...
Warning: python 2.7.13 is already installed, it's just not linked.
You can use `brew link python` to link this version.
$ brew link --overwrite python
$ which python
/usr/local/bin/python
$ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/Cellar/python/2.7.13...
Its no longer /Library/.. but /usr/local.
Now its finding all of my pip installed modules! Problem solved! ]]
The above Steps are actually dealing the merging of similar version ~ 2.7 which is of Python2.
But in my machine, i had installed Python3 and it has Python2 earlier.
Here is my question.
Do i have to update the System's version first to Python3 and then link it with Brew's version(3.9.15) by following the above commands or Any suggestions please??
Do i have to update the System's version first to Python3 and then link it with Brew's version(3.9.15)
python2 and python3 are incompatible software. You shouldn't try to "update" that version to python3. Because if you do that existing software might stop working, as it probably depends on python2. Your best course of action would be to use python3 version from brew explicitly, python2 is in deprecation mode anyway. (I use alias like alias py3=python3, so I can invoke it with py3 instead of writing python3)
As an aside - you should set up your projects/programs using virtualenv/venv; that will allow to not pollute your system installation. You can even share that with another computer by extracting project dependencies in requirements.txt.

How to install pip3 for a specific python version

I have python3.7 installed but also python3.4. For example:
$ python3
Python 3.4.3 (default, Nov 12 2018, 22:25:49)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
And:
$ python3.7
Python 3.7.0 (default, Jun 28 2018, 00:00:00)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
How can I install packages to python3.7 ? Any pip3 command I use goes to python3.4 and I'm not sure how to install anything for python3.7. Any ideas? Even running something like this doesn't work:
$ python3.7 -m ensurepip --upgrade
/usr/bin/python3.7: No module named ensurepip
Or:
$ sudo python3.7 -m pip install PyMySQL
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
I don't know whether this may be an appropriate solution for you. But this is what I generally follow.
Just install Anaconda in your system and create different environments according to your needs.
For your case create two different environments one for Python 3.4.3 and another for Python 3.7 using the following command
conda create --name py34 python=3.4.3 and
conda create --name py37 python=3.7
//This lines will create two new environments named py34 and py37
You then install libraries according to your needs in the respective environment. Now you can work into each environment without interfering with the libraries of the other environment.
To use anaconda kindly follow Anaconda cheatsheet. You will get everything that you need.
Hope this will help you.
This might help : It addresses the same issue as yours. In short, try
python3.7 -m pip install pip
Here's a reference documentation

Pandas unrecognized by Python3

I am using python3 in CentOS and I was able to install pandas using pip3, but when I do import pandas in python3, it throws import error: no module named pandas.
There are many reasons to not install packages through pip only, one is that you may end up installing them globally, meaning you either wont be able or will have trouble using two different versions of the same package.
It's better to let each project have it's own dependencies, if something goes really (really) wrong you'll just nuke your project environment without hurting other projects. One of the most acceptable ways of doing this is by using virtual environments.
With virtualenv
To create a virtualenv
$ virtualenv ENV
Then activate it
$ source bin/activate
Now install pandas
$ pip install pandas
To leave the environment:
$ deactivate
With pipenv
You can also combine pyenv (which lets you install different Python versions) with pipenv.
Example:
# creating a directory for my project
$ mkdir pandas-env && cd pandas-env
# creating an environment with Python 3.6.4
$ pipenv --python 3.6.4
# installing pandas in your environment
$ pipenv install pandas
# enter your environment
$ pipenv shell
# and check your python version, it's probably different
# from the older system's version
$ python
Python 3.6.4 (default, Mar 6 2018, 10:29:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
# now import pandas
>>> import pandas as pd
>>>
To leave your environment just press CTL+D.

QGIS3 won't install on mac with python3.6 already installed through Ananconda

QGIS installer keeps telling "QGIS requires Python 3.6." after which it quits installing on Mac. However I have python 3.6.4 at least on 4 locations
1) ~/anaconda/bin/python
2) /usr/bin/python3
3) /usr/local/bin/python
4) /usr/local/bin/python3.6 (through a symbolic link).
All these files refer to the same file, when invoking them they all yield:
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Does anyone know where QGIS3 installer looks for python 3.6, so I could set a symbolic link to the python 3.6 version that is already installed through Anaconda?
Thanks, Theo
Ran into this problem myself. Creating a symbolic link to where the installer expects to find python works. I had to find where Homebrew installed Python... (note the version in the path)
sudo ln -s /usr/local/Cellar/python/3.6.5/Frameworks/Python.framework /Library/Frameworks/Python.framework
I had python 3.7 and QGIS 3.X was looking for python 3.6.
error: "qgis requires python 3.6"
to downgrade I tried many things but finally "brew prune" and installing 3.6 directly from python.org worked for me.
https://www.python.org/downloads/release/python-366/
I had to install python 3.6 directly downloaded from www.python.org (ignoring the already installed version by Anaconda). This worked. See remark on http://www.kyngchaos.com/blog/2018/20180315_qgis_3_must_use_python.org_python_3
Install via the terminal with
conda install -c conda-forge qgis
I had the same issue and could only install QGIS with this solution. https://anaconda.org/conda-forge/qgis
According to this answer, the path is in:
/usr/local/Cellar/python3/3.x.y_z/Frameworks/Python.framework
I installed Python 3.6.5 from the main Python webpage ignoring the existing Anaconda version that I use regularly. Then once it is completed I checked my PATH and I had both directory
echo $PATH
/Library/Frameworks/Python.framework/Versions/3.6/bin:/anaconda3/bin:
You can check the folder of python using which python3 and it should give you the Frameworks.
I installed the GDAL and then QGIS without any problems. I run QGIS and it is working, until now :)
Remember: you will need to use pip3 to install any missing modules other wise it you will be calling the pip under anaconda.
owslib, PyYaml, psycopg2, jinja2, pygments, numpy, plotly.
pip3 install modulename

python3 - No module named 'html5lib'

I'm running a python3 program that requires html5lib but I receive the error No module named 'html5lib'.
Here are two session of terminal:
sam#pc ~ $ python
Python 2.7.9 (default, Mar 1 2015, 12:57:24)
[GCC 4.9.2] on linux2
>>> import html5lib
>>> html5lib.__file__
'/usr/local/lib/python2.7/dist-packages/html5lib/__init__.pyc'
>>> quit()
sam#pc ~ $ python3
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
>>> import html5lib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'html5lib'
>>>
Where can be the problem?
Seems you have the module only for python 2. Most probably need to install it for python3. Usually use pip3 for that.
pip3 install html5lib
You can check your installed modules using:
pip freeze (or pip3 freeze)
I strongly recommend you to use virtualenv for development. So you can separate the different python versions and libraries/Modules by project.
use:
pip3 install virtualenv
You can then easily create "environments" using (simple version)
virtualenv projectname --python=PYTHON_EXE_TO_USE
This creates a directory projectname. You just switch into that dir and do a
Scripts\activate (on linux/unix: source bin/activte)
And boom. You have an isolated environment with the given python.exe and no installed modules at all. You also have an isolated pip for that project. Really helps a lot.
To end working in that project do a:
Scripts\deactivate (on linux: deactivate)
Thats it.
ONe moer thing ;) You can also do a
pip freeze > requirements.txt
to save all needed dependencies for a project in a file.
Whenever you need to restart from scratch in a new virtualenv you cabn simply do a:
pip install -r requirements.txt
This installs all needed modules for you. Add a -U to get the newest version.

Resources