IPython import error - pattern - python

I'm a newbie to IPython/Anaconda (for Windows 7), and for some reason, I'm getting an import error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-6-484073d472a5> in <module>()
6 import matplotlib.pyplot as plt
7 import requests
----> 8 import pattern
ImportError: No module named pattern
Here's what I did to set up Anaconda/IPython:
Installed Anaconda. I already have a version of python on this computer. The Anaconda distribution was installed to C:\Users\MyName, while the regular python distribution (2.7) was installed to C:\Python27.
When I tried to import some packages (sklearn, pandas, requests, pattern), for some reason those packages just wouldn't import so I installed them manually (downloading them to a local folder on my desktop and running $ python setup.py install, and after that it all seemed to work).
I can run IPython notebooks fine. However, when I try to import pattern specifically, I get that error (which I don't get for sklearn or any of the other packages I installed manually). Printing my sys path from the terminal (import sys print sys.path) and IPython notebook gives:
Terminal:
['C:\\Users\\MyName\\Desktop\\Data Science\\cs109\\Labs', 'C:\\python27\\lib\\site-
packages\\mrjob-0.4.2_dev-py2.7.egg', 'C:\\python27\\lib\\site-
packages\\simplejson-3.3.1-
py2.7.egg', 'C:\\python27\\lib\\site-packages\\pyyaml-3.10-py2.7-win32.egg',
'C:\\python27\\lib\\site-packages\\boto-2.18.0-py2.7.egg',
'C:\\python27\\lib\\site-packages\\requests-2.0.1-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\lib\\site-packages\\Orange\\orng', 'C:\\python27\\DLLs',
'C:\\python27\\lib', 'C:\\python27\\lib\\plat-win', 'C:\\python27\\lib\\lib-tk',
'C:\\python27', 'C:\\python27\\lib\\site-packages', 'C:\\python27\\lib\\site-
packages\\PIL', 'C:\\python27\\lib\\site-packages\\win32',
'C:\\python27\\lib\\site-
packages\\win32\\lib', 'C:\\python27\\lib\\site-packages\\Pythonwin']
IPython notebook:
['', 'C:\\Users\\MyName\\Anaconda\\python27.zip', 'C:\\Python27\\lib\\site-
packages\\Orange\\orng', 'C:\\Users\\MyName\\Anaconda\\DLLs',
'C:\\Users\\MyName\\Anaconda\\lib', 'C:\\Users\\MyName\\Anaconda\\lib\\plat-win',
'C:\\Users\\MyName\\Anaconda\\lib\\lib-tk', 'C:\\Users\\MyName\\Anaconda',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\PIL',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\win32',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\win32\\lib',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\Pythonwin',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-
info', 'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\IPython\\extensions']
If I do
import pattern
print pattern
It shows that pattern is located at:
C:\python27\lib\site-packages\pattern\__init.pyc
Any thoughts as to why IPython can't import pattern?

I struggled with this - I kept getting an error saying package couldn't be found.
Running below in command prompt worked for me.
conda install -c asmeurer pattern=2.5

On windows, open cmd.exe and type:
conda install pattern
This should do it ;)

Sometimes this happens when you have multiple versions of Python/Anaconda installed on your machine. As the Pattern package does not run on Python 3.4, you need to launch IPython Notebook from the Anaconda server that runs Python 2.7.
So the first step is to make sure that you install the Pattern package using pip in the version of Anaconda that runs Python 2.7.
For example, C:\Users\MyName\Anaconda\Scripts\pip install pattern
The second step is to make sure that you run ipython notebook from the correct path.
For example, C:\Users\MyName\Anaconda\Scripts\ipython notebook
That should do it.

Related

VS Code is not recognizing 'nmap'

I am trying to make a nmap scanner for the InfoSec Certification on freeCodeCamp.org and cannot get Visual Studio Code to recognize that I have installed nmap. I am very beginner and in the process of learning.
from cProfile import run
import nmap
scanner = nmap.PortScanner()
print("Welcome, this is a simple automattion tool")
print("<------------------------------------------->")
When I run this in VS Code I get the following in the terminal:
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1> python3 scanner.py
Traceback (most recent call last):
File "C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1\scanner.py", line 2, in <module>
import nmap
ModuleNotFoundError: No module named 'nmap'
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1>
I have so far:
Updated to the current Python 3.10.7
Installed Nmap the first time from https://nmap.org/ for Windows
Uninstalled Nmap
Reinsalled Nmap using >>>pip3 install python-nmap
For future searchers (as in my comment above), if you installed a module with pip3 but are still getting module import errors, try python3 -m pip install <module-name>.
Not sure how/why this happens (pip3 installing somewhere that python3 is not looking for modules - maybe PYTHONPATH-related), but the above can [usually] help.
It seems that Python cannot find nmap module.
You can either add it to path in Windows via Start -> Edit system environment variables -> Environment variables... and then edit PATH VARIABLE by adding at the end path to nmap module that you have installed or move the module to default Python modules directory which should be C:\Users\YourLogin\AppData\Local\Programs\Python\Python310\Lib\site-packages

ImportError when importing in python notebook, but import works fine in terminal?

The import works when I call it from the command line, but when I call the import in the ipynb I get the following error:
ImportError: libmkl_intel_lp64.so: cannot open shared object file: No such file or directory
I have already added the path to libmkl_intel_lp64.so to my bashrc file, so I am not sure what else I can do to fix this import error in the notebook.
Your command line python interpreter,
and your jupyter notebook kernel,
have different environments.
Use one of these techniques to help you debug the path problem:
$ python -m site (from bash prompt)
import sys; from pprint import pp; pp(sys.path) (within a cell)
Do yourself a favor, and use conda to manage your environment.
https://docs.conda.io/en/latest/miniconda.html
Be sure to conda activate myfavoriteenvironment
before you start the jupyter kernel.

Mac OS VSCode built-in terminal can't import installed python packages even the corrected conda environment selected

Mac OS VSCode built-in terminal can't import installed python packages.
I've installed NumPy correctly using Anaconda in one of my environments called tf20;
And then I created a python script scene.py
import numpy as np
print(np.__version__)
I can run this script in my system Terminal like:
(tf20) mac#Macbook:~/Desktop/manim_project$ python scene.py
1.18.5
However, an import error occurred when I run the same script.
(tf20) mac#Macbook:~/Desktop/manim_project$ python scene.py
Traceback (most recent call last):
File "scene.py", line 10, in <module>
import numpy as np
ImportError: No module named numpy
However, when I run this by pressing F5, the debug mode, the problem disappeared.
(tf20) mac#Macbook:~/Desktop/manim_project$ cd /Users/mac/Desktop/manim_project ; /usr/bin/env /Users/mac/.jackprogram/anaconda3/envs/tf20/bin/python /Users/mac/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/lib/python/debugpy/launcher 62372 -- /Users/mac/Desktop/manim_project/scene.py
1.18.5
(tf20) mac#Macbook:~/Desktop/manim_project$
I think I selected the right python environment tf20 in VSCode because
the VSCode built-in terminal showed (tf20) at the beginning of the terminal prompt.
the left corner of VSCode showed Python3.7.7 64-bit ('tf20:conda').
and I even used cmd + shift + p then Python: Select Intereter to ensure I selected the right python environment.
Because of this problem, I have to use the macOS terminal application to run the Python scripts for a half year.
Help. Please!

Python: modul not found after Anaconda installation

I've successfully installed Python 2.7 and Anaconda but when i try to import a library i get always this error:
>>> import scipy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named scipy
I've set up the PYTHONHOME to C:\Python27 and PYTHONPATH to C:\Python27\Lib.
EDIT : content of PATH
In my $PATH variable i have C:\Users\Mattia\Anaconda2, C:\Users\Mattia\Anaconda2\Scripts and C:\Users\Mattia\Anaconda2\Library\bin.
Do i have to set any other env veriables?
The problem is that you should not have either PYTHONPATH or PYTHONHOME set. They are both pointing to a non-Continuum version of Anaconda, I believe. Anaconda will install (by default) into a directory called Anaconda, either at C:\Anaconda or at C:\Users\USERNAME\Anaconda (IIRC). It is generally recommended that you should not ever set PYTHONPATH or PYTHONHOME, except as a last resort, exactly because of these kinds of problems.
You can see which Python interpreter you're running by doing:
>>> import sys
>>> sys.executable
And then you can see what directories are ending up in your Python library path (where import statements will look for packages, such as scipy and numpy) by doing one of the following:
>>> import sys
>>> sys.path
or the more readable version:
>>> import sys
>>> for p in sys.path:
... print p
As pointed out by #Mr.F the error was given by the presence of the PYTHONPATH and PYTHONHOME. Deleting them i was able to use the Anaconda version of python.
If you have module not found error, you may need to launch python from anaconda terminal using "python" instead of the shortened "py". I had my module installed properly, but spent forever trying to fix it because of this. Apparently py does not launch the anaconda activated or anaconda base environment, but launches another version of python.
Use
$ conda install package_name instead

Error to launch IPython shell after source install

I have an account in a CentOS server without sudo permission. As a result, I tried to install IPython from source code by python setup.py prefix=<my home directory>.
However, I got the Error:
Traceback (most recent call last):
File "/usr/bin/ipython", line 19, in <module>
from IPython.frontend.terminal.ipapp import launch_new_instance
ImportError: No module named IPython.frontend.terminal.ipapp
I found a question same to mine: IPython import failure and python sys.path in general.
I followed the asker's instruction to add the directory of my IPython to the IPython execution file.
#!/usr/bin/python
"""Terminal-based IPython entry point.
"""
import sys
sys.path.append("./Ipython directory")
from IPython.frontend.terminal.ipapp import launch_new_instance
launch_new_instance()
However, I got the same error as well. So I want to know how can I launch IPython correctly? Thanks.
You probably want to install with the --user flag, which should put it on sys.path automatically.
python setup.py install --user
If you need to use --prefix for some other reason, then make sure that the directory you add to sys.path is the folder containing the IPython package, not the IPython directory. It will probably end with 'site-packages'.
I copied the IPython source folder into ~/bin/. The execution file of IPython can recognize the module in IPython and the IPython shell launches successfully.

Categories

Resources