How to import a module from a directory? - python

On my system I have two versions of Python (to call them I type python and python2 in the command line). When I use the first version of Python, I cannot import sklearn module but I can do it in the second version of Python.
I would like to use the first version of python (because other modules are available there) and, at the same time, I would like to be able to import sklearn from this version of Python.
My solution was to use:
import sys
sys.path.append('location_of_the_sklearn_module')
To find the location of the sklearn module I started a python session (using the second version of python, in which sklearn works). The I type:
import sklearn
sklearn.__file__
As a result I got:
/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn/__init__.pyc
In the session of the first version of Python I tried:
import sys
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
import sklearn
Unfortunately it did not work. As a result I got: ImportError: No module named sklearn
Does anybody know what I am doing wrong and if it is possible to reach the goal in the way I try?

When importing packages, you need to add the parent directory of the package to PYTHONPATH, not the package directory itself, so just change...
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
...to...
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages')
...although it may not necessarily import correctly in Python 3.x.

Related

Check version of imported library in python script

in a python file, how to check the version of all the libraries that are imported exactly in that file by "import xxxxxxxxxxxx".
This is useful to later make an environment that has enough library to run that script while don't need to clone the exact environment.
example:
import pandas
import tensorflow
def version_list():
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# expect the version_list to return
# pandas==xxx tensorflow==xxx
# without returning other things
Many modules have their version set to the variable __version__. For example
import numpy
print(numpy.__version__)
prints the version of numpy which you have imported.
This may be related: Standard way to embed version into Python package?

ImportError: cannot import name 'array2d'

Code:
from sklearn.utils.validation import array2d
Output:
ImportError: cannot import name 'array2d'
I have installed sklearn v0.23.1 with pycharm.
I tried to look in site-packages\sklearn\utils\validation.py and find "array2d" with ctrl+F, but it is not there. Can I import something when it is not in the file?
I am not sure, if the problem is in installation or import.
According to the documentation of the latest stable version (currently 0.23.1), there is no module array2d in sklearn.utils.validation.
The situation is the same for the previous versions 0.22.2, 0.21.3, 0.20.4, 0.19.2, and 0.18.2.
Not sure where you got this from and what you are trying to do - googling the error leads to this closed Github thread, where the usage is not like the one you try here (and the error there comes from a broken installation, which is certainly not the case here).
In short, the command from sklearn.utils.validation import array2d is invalid, and the error thus justified.

pythonnet clr how to add reference to a dll assembly?

I am trying to import a dll package into python through pythonnet clr. I am aware that the package CLR and pythonnet both end up having a namespace called clr so the command "import clr" can be equivocal. Long story short, I seem to need pythonnet not the other one. I would like to be able to specify the address of the dll assembly; this one works:
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
Apath=os.path.normpath("C://Folder//Folder//Folder//AssemblyA.dll")
clr.AddReference(Apath)
but this one fails (got the idea of this one from here):
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
BfolderPath=os.path.normpath("C://Folder//Folder//Folder")
sys.path.append(BfolderPath)
clr.AddReference('AssemblyB.dll')
I get the following error when I try running the second one:
"System.IO.FileNotFoundException: Unable to find assembly 'AssemblyB.dll'.
at Python.Runtime.CLRModule.AddReference(String name)"
this one also fails
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
BfolderPath=os.path.normpath("C://Folder//Folder//Folder")
clr.AddReferenceToFileAndPath(Bpath)
"AttributeError: module 'clr' has no attribute 'AddReferenceToFileAndPath'"
ps1. I need the second or the third way to work because I have to be sure the second assembly is not confused with another one with a similar name.
ps2. I cannot find the documentation of pythonnet or see what kind of commands are available in my clr. Any idea?
Any tip is appreciated.
In second idea,
remove .dll from clr.AddReference('AssemblyB.dll') and use clr.AddReference('AssemblyB') because clr.AddReference() Requires only assembly name whether is it .exe of .dll not the folder path
That's why first idea is not working!
And for third idea clr.AddReferenceToFileAndPath() is not working because it part of Ironpython not pythonnet.
Ironpython is also used like pythonnet, but Ironpython is using Managed python code, while pythonnet is not using Managed code.

ImportError for skbio module

I am running Python 3 and have skbio v0.5.5 installed. Following the examples in this tutorial, I am trying to run the import statements for some skbio classes, but am getting errors. For example,
from skbio.alignment import Alignment
results in
ImportError: cannot import name 'Alignment' from 'skbio.alignment'
Also,
from skbio import BiologicalSequence
results in
ImportError: cannot import name 'Alignment' from 'BiologicalSequence'
How do I resolve this?
The Alignment class is from an older version of the skbio library, specifically from before version 0.3.
If you would like to use those classes, you would need to install scikit-bio from before that version, by doing something like:
pip install scikit-bio==0.2
If you're just going through the Introduction to Applied Bioinformatics book, like you mentioned in your comment, it's better to use the latest version of the book instead, which will use the latest version of the scikit-bio library.

iPython (python 2) - ImportError: No module named model_selection

iPython Notebook
Python 2
Complaining about this line:
from sklearn.model_selection import train_test_split
Why isn't model selection working?
In order to remedy this issue, you need to first find out if you are importing the actual sklearn package, and not just some script with the name sklearn.py saved somewhere in your working directory. The way Python imports modules is somewhat similar to the way it finds variables in its namespace (Local, Enclosed, Global, Built-in). In this case, Python will start importing module by first looking in the current directory and then the site-packages. If it looks in the current working directory and finds a python script with the same name as the module you are trying to import, then it will import that script instead of the actual module.
You can usually find out whether or not the actual module is imported by checking its __file__ or __path__ attribute:
import sklearn
print(sklearn.__file__)
print(sklearn.__path__)
Reviewing the output of those print statements will tell you whether the imported package is the module you are after, or just some script lying somewhere in your working directory. If, in case, the output does not point to the site-packages of your Python version then you have imported some script somewhere that's not the module itself. Your quick fix would be to exit the console first, rename the .py script and its compiled version (the .pyc file) and go back to your console and try again.
However, if the output points to your python version's site-packages, then there is something wrong with how the package was installed in the first place. In which case, you will probably need to update or reinstall it.
Particular to this, it turns out that the issue is with the version of sklearn you are using; because the model_selection module in sklearn is available in versions 0.18+. If you're using a version number (sklearn.__version__) lower than 0.18, then you will have to use the old cross_validation module instead of the model_selection module:
from sklearn.cross_validation import train_test_split
You can also just upgrade to the latest version of the package with your preferred package management system.
I hope this is helpful.

Categories

Resources