ImportError: cannot import name 'moduleTNC' - python - python

I'm having a problem in python when I try to import linear_model from sklearn library: from sklearn import linear_model. I have just installed it via pip simply in this way: pip install sklearn. I know that to avoid this error suffices uninstall and reinstall sklearn, but it didn't work. I also installed it via conda but opening the idle (is that correct?) it gives the same error.
How to avoid it?
NB: If I use jupyter from conda it works well as it should.

I had this same problem and resolved it with:
conda remove scipy scikit-learn -y
conda install scipy scikit-learn -y
I saw it here, where many other people said it solved their problems too.

With regards to the following error:
ImportError: cannot import name 'moduleTNC'
It can be solved by renaming moduletnc.cp36-win_amd64.pyd with moduleTNC.cp36-win_amd64.pyd in:
AppData\Roaming\Python\Python36\site-packages\scipy\optimize

I can't mark as possible duplicate, so I'm just pasting here. If that's a wrong behaviour, I'm sorry:
Import module works in Jupyter notebook but not in IDLE
The reason is that your pip/conda installed library paths are not accessible by python IDLE. You have to add those library paths to your environment variable(PATH). To do this open my computer > properties > advanced system settings > system.
Under environment variables look for PATH and at the end add the location of installed libraries. Refer this for more details on how to add locations in path variable. Once you do these you will be able to import the libraries. In order to know what locations python searches for libraries you can use
import sys
print sys.path
This will give you a list of locations where python searches for libraries. Once you edit the PATH variable those locations will be reflected here.
Refer this also in order to know how to add python library path.
Note: The tutorial is a reference on how to edit PATH variable. I encourage you to find the location of installed libraries and follow the steps to edit the same.

Related

query what libraries are on the host of the python notebook

In a jupyter notebook, I can fairly easily check with python code if some libraries are installed on the current kernel of the python notebook.
However there is also the "host" kernel which has its own python env (ie the python process that was launched when jupyter notebook was called). Depending on what libraries/extensions were installed on the host, it may not be possible to do specific things on the jupyter notebook client itself.
Is there a way to query what libraries/modules/extensions are installed on the host, from the "client" notebook ? thanks
Did you test !pip list or !pip freeze?
To list locally installed packages and their version # within a pipenv environment, cd into a pipenv project and enter the following command:
pipenv lock -r
I guess you can use !pip list to show the modules/libraries installed on the current env. But I don't think you can view extensions.
Please use !pip list --local or !pip freeze --local if you're using a virtual environment.
This could get you started:
import sys
import os
for i in sys.path:
try:
print("->", i, ":")
print(repr(os.listdir(i or ".")))
except Exception:
print("can't list", i)
pass
NOTE that my code isn't a complete solution, but it is a start. I will attempt to improve it shortly, but even as it is it may be immediately useful for you to develop your own solution.
What it doesn't do:
it does not determine whether it found a python file or a directory containing a valid package or something that isn't either (i.e. is not 'import'-able)
It skips over zipped packages (such as this item that could be in sys.path: /home/pyodide/lib/python39.zip)
it does not attempt to find built-in packages (those that are compiled into Python itself). Most of those would be already loaded, so you will find them in sys.modules, though
If you need to test for individual packages, rather than get a whole list: you may be better off using 'import' inside a try/except statement:
try:
import something
except ImportError:
pass
# do what you want if the package isn't available
If I understand correctly, you can run:
help("modules")
Or you can use pydoc for your work:
import pydoc
!pydoc modules
I believe !pip list should work- if not, then pydoc most likely would, see above.

pip install, How to fix ImportError

I'm having ImportError: cannot import name 'Literal' from 'typing' error message when I try to use pip install. Anyone can help?
The problem is with the configuration of your environmental variables. The first thing, I'd suggest you do is uninstall all the unused versions of python. To my mind, the latest of python 3.x.x series is the best.
Steps:
The easiest way of accessing it is just to type environment variable in Windows 10 search bar. (Assuming you are using Windows 10)
Then follow these:
At the bottom half part of the subsequent window search for path:
If you double click that, look for python. Make sure you have such paths added there and you need to have a compatible version of python. Usually, your python bin directory is located in Program Files.

So i installed numpy . But when i call it in a program an error occurs. Any method to solve it permanently in windows 10

Here is the error
import numpy
Exception has occurred: ModuleNotFoundError
No module named 'numpy'
File "C:\path\to\file\32.py", line 1, in <module>
import numpy
Let me know how did you install the NumPy package; using pip or something else?
If you have multiple python versions, i.e. 2.x and 3.x at the same time, please make sure your interpreter for the 32.py file is the version that you installed NumPy on.
To possibly fix your problem, you should first try installing it and see if there are any errors. You should also check the version of Python you are running on Windows 10, because when you update Python it sometimes switches names between py and python
As you can see, the version of Python has changed between py and python so you should try changing that first.
If this does not work, you should try finding the directory for NumPy and adding it to the system PATH in your script. The installer usually shows you the location by doing the following:
import sys
sys.path.append("<insert numpy location here>")
import NumPy
This should manually force it into finding the package. If none of this works, please tell us and we should be able to find a different solution.
Happy Coding!
If you're using a code editor like PyCharm, you could install it by clicking on
file then settings then the project interpreter setting and install new module! You can search for the module and install.
Make sure that the python version that you want to use is a Windows Environmental Variable. You can test this by running this line in your command line.
> python --version
If you get some other python version that is not the one that you wish to use you can set the python version you want by finding where exactly your Python folder is located and go into settings and set the path as a new variable (I can leave a tutorial for you). If that is too much of a hassle, the Python installers can set the python that you will install as an environmental variable for you. (You could uninstall and reinstall and make sure that you allow it to make it an environmental variable.
After that you should be able to import whatever external packages you want using pip
for example:
pip install numpy

Jupyter-notebook returns "module not found" for every single import, despite them being downloaded

For example, import trackpy returns the module not found error.I have already confirmed that trackpy has been downloaded somewhere on my computer, because attempting to install it again via conda install -c soft-matter trackpy will eventually return something to the effect of "all files already installed". This seems to occur for every "external import" (numpy, scipy, matplotlib), i.e. one that was downloaded somewhere from the internet. This does not happen for "internal imports" (sys, os). I believe this is just a matter of jupyter not looking for the files in the correct place, but I don't know how to fix something like this.
Edit: Relevant info: I ran
import sys
sys.executable
which returns 'c:\\users\\reese\\miniconda3\\python.exe'. In the pkgs folder for miniconda3, there are none of the imports that I want. However in 'c:\\users\\reese\\Anaconda\\pkgs' are all the imports, trackpy and all else. Is there an easy way to make jupyter check here for imports? I already tried straight up copying the entire pkgs folder and pasting it in miniconda3's pkgs folder, but it did not work.
Two solutions I would propose.
Okay Solution:
Yes, you can add the path to your other packages with sys.path:
import sys
sys.path.insert(0,'PATH_TO_YOUR_OTHER_PACKAGES')
import Packages_of_another_path
By insert it at index zero, you ensure that your other packages get first priority in case there in another package with the same name.
Better Solution:(Recommendable)
Always use environments. E.g.
conda create —name your_env python=3.6 pip
conda activate your_env
conda install packages1 packages2
pip install package3
In this environment you can keep all you things together.
Everything you wish to use your packages, activate your environment and start hacking ;)

How to fix 'ImportError: dynamic module does not define module export function (PyInit_cv2)' error in Python?

I'm running a code on deep learning, which uses the opencv module, by running python main.py (contains import cv2 statement), but always get the error 'ImportError: dynamic module does not define module export function (PyInit_cv2)'.
I've tried to reinstall my anaconda and create new virtual environments, but all got the same result. This problem really confuses me a lot and I've googled for many related problems, none of them works. I think the problem is something related to the environment and has nothing to do with the code, because I got the same result by simply run import cv2 in python prompt. The more confusing thing is that, even after I remove the opencv module, I also get the same problem, but not a ModuleNotFoundError. Does anyone can give me some advice? Thanks a lot!
I think I found one possible reason of this error.
Recently I was configuring the caffe environment on one server, I downloaded the source code of opencv-2.4.13 and compiled manually, added /usr/local/opencv-2.4.13/build/lib to $PYTHONPATH, and caffe worked well. After that, when I entered one of my virtual environment using conda activate py35, which uses python3.5, tried import cv2 in the python prompt, got the error above.
I'm not sure but I think the cause of the error is opencv-2.4.13 compiles a python2 interface so it can't be imported by python3. Python imports packages by searching the directories listed in sys.path, where $PYTHONPATH is in the second place after the current working directory (This is a great article introduces the mechanism of python finding packages). So when we enter the py35 environment, python will first look for $PYTHONPATH and find the opencv installed on the root directory instead of finding the opencv in the virtual environment using conda install opencv-python.
So there are two solutions of this problem:
Use python2 instead.
Remove /usr/local/opencv-2.4.13/build/lib from $PYTHONPATH.
which all work for me.
Similar post, might help:
ImportError: dynamic module does not define init function (initfizzbuzz)
Could you provide info on how you installed the CV module?
I had the same problem, which was caused by the cv2.so file in /usr/local/lib/python2.7/site-packages/cv2.so. After I deleted the file and use command sudo pip3 install opencv-python, it worked for python3.

Categories

Resources