scons cannot import numpy module - python

I have a sconstruct file and i am trying build a process.
A part of my code is below.
# Import modules needed by Scons
import os
import sys
# Create an Scons Environment
env = DefaultEnvironment()
env.Decider('MD5-timestamp')
sys.path.append(r"C:\Python27\Lib\site-packages")
sys.path.append(r"C:\Python27\Lib\site-packages\numpy")
sys.path.append(r"C:\Python27\Lib\site-packages\numpy\linalg")
import numpy
When i try to run scons, it complain about not able to find some sub module of numpy such as lapack_lite, _umath_linalg. The screenshot of the error attached.
I have checked this files inside my site-pacages. It is defintely present inside the folder.
When i import numpy library from python, i dont have any problem.

I had a dependency issue.
The only solution that worked was to completely remove python, all its libraries.
Reinstall python, libraries and scons back. Made sure all pythonpath and sys path are set properly.
It started to work

Related

Import [Module] could not be resolved (PylancereportMissingImports), with module in the same folder/directory

The first few lines of the code of evaluation.py:
import os
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
import numpy as np
from dataset import CLSDataset # warning is reported here
from tqdm import tqdm
The structure of the folder:
./
|-dataset.py
|-dictionary.py
|-evaluation.py
|-model.py
|-models/
|-[some files]
|-__pycache__
|-train.py
Notice that dataset.py is in the same folder as that of evaluation.py and https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#unresolved-import-warnings says that The language server treats the workspace root (i.e. folder you have opened) as the main root of user module imports. But it still throws an warning of "Import dataset could not be resolved".
I tried to add the
{
"python.analysis.extraPaths": ["./"]
}
on the settings.json of both local and remote files, but it does not help.
In VSCode, go to the main window and do the following:
Do Ctrl+Shift+P (for Windows) and Command+Shift+P (for Mac)
Scroll and go to Python Select Interpreter
Now select whichever python version is installed in your system.
You're good to go!
Do upvote if it helps you!
Dataset is a relative import.
Add an __init__.py file your directory (a file with no content). Then try:
from .dataset import CLSDataset
That being said, Python imports are a tricky business. It looks like we’re only getting a glimpse of one part of workspace setup.
(I am surprised that your code runs as-is.)
Consider scaffolding your project with cookiecutter. Also try out the vs code python project tutorials. You don’t have to remake your entire project but these will give you a starting point for understanding where your current project goes awry.
I recently had the same error, just try restarting the IDE. You probably installed it thought the code was already open. Or you haven't installed the module at all.
Edit:
I'm sorry I understood it wrong, please send the code.
Edit2:
You need to add the .py at the end of the import if it is a .py file

How to use local python module>

I want to import a python module I have off of Github, but I want it to be portable, I.E. available to use on a memory stick, and I want to have it so I don't have to install the module through CMD on every machine I want it to run on.
I've seen Import python package from local directory into interpreter, but none of the answers worked for me, as I don't have a specific file to target, it's a directory I want to import. The module I want to import is (https://github.com/ricmoo/pyaes)
Try something like this
import sys
sys.path.append('C:\\Users\\Administrator\\Desktop\\pyaes-master')
import pyaes
Have you tried
from [path to directory] import [whatever you want to import]?
That should do the trick.

How to make an external module local

My code depends on functions from a module external_module which is in my pythonpath path and which I include as
# global import
import external_module.sub_mod_one as smo
Now I want to share my code but I don't want to force my collaborators to checkout my other git repos and add them to their environment.
So, I thought I can copy the files to the local directory and rewrite the import as
# local import
import sub_mod_one as smo
However, since development goes on, I don't want to do this manually.
Question Is there a python module or vim plugin or something else that does this for me? Namely, copying the the included modules to the current folder and rewriting the import statements?
The "right" solution is to
properly package your "external_module" so it can be installed with pip,
add to your project(s) a pip requirements file referencing your package
then have everybody using virtualenvs
This way the package will be cleanly installed (and at the right version), you don't have to mess with your exports, and you dont have out of sync copies of your package everywhere.
You could use conditional imports:
try:
import external_module.sub_mod_one as smo
except ImportError:
import sub_mod_one as smo

No module named when using PyInstaller

I try to compile a Python project under Windows 7 using PyInstaller. The project works fine, there are no issues, however when I try to compile it the result doesn't work. Though I get no warnings during compilation there are many in the warnmain.txt file in the build directory: warnmain.txt
I don't really understand those warnings, for example "no module named numpy.pi" since numpy.pi is no module but a number. I never tried to import numpy.pi. I did import numpy and matplotlib explicitly. In addition I'm using PyQt4. I thought the error might be related to those libraries.
However I was able to compile a simple script which uses numpy succesfully:
import sys
from PyQt4 import QtGui, QtCore
import numpy as np
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.pb = QtGui.QPushButton(str(np.pi), self)
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
Successfully here means that the created executable file actually showed the desired output. However there is also a warnmain.txt file created which contains exactly the same 'warnings' as the one before. So I guess the fact that compiling my actual project does not give any success is not (or at least not only) related to those warnings. But what else could be the error then? The only output during compilation are 'INFO's and none of the is a negative statement.
I did not specify an additional hook directory but the hooks where down using the default directory as far as I could read from the compile output, e.g. hook-matplotlib was executed. I could not see any hook for numpy neither could I for my small example script but this one worked. I used the following imports in my files (not all in the same but in different ones):
import numpy as np
import matplotlib.pyplot as ppl
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from PyQt4 import QtGui, QtCore
import json
import sys
import numpy # added this one later
import matplotlib # added this one later
Since PyInstaller does not give any errors/warnings I could not figure out if the problem is related to the libraries or if there is something else to be considered.
Had a similar problem with no module named FileDialog. Discovered that with version 3.2, I could use
pyinstaller --hidden-import FileDialog ...
instead of modifying my main script.
See Listing Hidden Imports documentation
Pyinstaller won't see second level imports. So if you import module A, pyinstaller sees this. But any additional module that is imported in A will not be seen.
There is no need to change anything in your python scripts. You can directly add the missing imports to the spec file.
Just change the following line:
hiddenimports=[],
to
hiddenimports=["Tkinter", "FileDialog"],
If you are getting ModuleNotFoundError: No module named ... errors and you:
call PyInstaller from a directory other than your main script's directory
use relative imports in your script
then your executable can have trouble finding the relative imports.
This can be fixed by:
calling PyInstaller from the same directory as your main script
OR removing any __init__.py files (empty __init__.py files are not required in Python 3.3+)
OR using PyInstaller's paths flag to specify a path to search for imports. E.g. if you are calling PyInstaller from a parent folder to your main script, and your script lives in subfolder, then call PyInstaller as such:
pyinstaller --paths=subfolder subfolder/script.py.
The problem were some runtime dependencies of matplotlib. So the compiling was fine while running the program threw some errors. Because the terminal closed itself immediately I didn't realize that. After redirecting stdout and stderr to a file I could see that I missed the libraries Tkinter and FileDialog. Adding two imports at the top of the main solved this problem.
I was facing the same problem and the following solution worked for me:
I first removed the virtual environment in which I was working.
Reinstalled all the modules using pip (note: this time I did not create any virtual environment).
Then I called the pyinstaller.
The .exe file created thereafter executed smoothly, without any module import error.
I had the same problem with pyinstaller 3.0 and weblib. Importing it in the main didn't help.
Upgrading to 3.1 and deleting all build files helped.
pip install --upgrade pyinstaller
If the matter is that you don't need Tkinter and friends because you are using PyQt4, then it might be best to avoid loading Tkinter etc altogether. Look into /etc/matplotlibrc and change the defaults to PyQt4, see the 'modified' lines below:
#### CONFIGURATION BEGINS HERE
# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
# Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
#modified
#backend : TkAgg
backend : Qt4Agg
# If you are using the Qt4Agg backend, you can choose here
# to use the PyQt4 bindings or the newer PySide bindings to
# the underlying Qt4 toolkit.
#modified
#backend.qt4 : PyQt4 # PyQt4 | PySide
backend.qt4 : PyQt4 # PyQt4 | PySide
May not be a good practice but installing pyinstaller in the original environment used in my project (instead of a separate venv) helped resolve ModuleNotFoundError
I had similar problem with PySimpleGUI.
The problem was, pyinstaller was installed in different directory.
SOLUTION (solved for me) : just install pyinstaller in the same directory in which the file is present (that to be converted to exe)
If these solutions don't work, simply deleting and reinstalling pyinstaller can fix this for you (as it did for me just now).
Putting this here for anyone else who might come across this post.
I had the same error. Mine said "ModuleNotFoundError: No module named 'numpy'". I fixed it by typing the following in the cmd:
pip install pyinstaller numpy

Make "shortcut" from Python module

Let's say I have vtk module in my Python site packages, and from application with own Python distribution I want to access this module.
I tried couple of things like:
import sys
sys.path.append("C:\Python27\Lib\site-packages")
sys.path.append("C:\Python27\Lib\site-packages\vtk")
import vtk
lut = vtk.vtkLookupTable()
but it fails to load module properly:
AttributeError: 'module' object has no attribute 'vtkLookupTable'
If I do same from default Python interpreter all is fine.
Now I thought to make a wrapper of vtk in this application site packages, with simple __init__.py resolving paths, so that when I do import vtk it will hopefully load right thing, but I have no experience with Python packages to try to make this work
To put it simple, how can I wrap module from arbitrary folder, in Python site packages by making folder with same name as referenced package and simple __init__.py file?
Remove these lines:
sys.path.append("C:\Python27\Lib\site-packages")
sys.path.append("C:\Python27\Lib\site-packages\vtk")
The site-packages will already be on your python path. Adding a package/folder within that python path (especially at the first level), will just mess with your imports. How is this vtk package structured?
/path/to/site-packages/
vtk/
__init__.py
vtk.py
In this case, to access a function within vtk:
from vtk import vtk
lut = vtk.vtkLookupTable()
It all comes down to how the folder is arranged. You could also do this:
import vtk
lut = vtk.vtk.vtkLookupTable()
Do not try to hack python importing by creating proxy modules simply because you're not understanding how python importing is working. The error was quite clear. The attribute vtkLookupTable did not exist on whatever it was you imported. You imported the wrong thing. Fix it.
You should very very very very rarely have to manipulate the sys.path manually. When you do have to, you should know that it's the right reason - not to work around something you're not fully understanding.
I had trouble with python paths when I first started with python. It can be frustrating, but coming to understand how it works is necessary. What can help you is something like the following:
import vtk
print dir(vtk)
That will print the attributes of vtk, so you can explore exactly what is in the package or module in cases like this where you think you're importing the right thing.
After re-reading your question, it seems like this is a different python install you're talking about. The answer is to install this package into the other python install, or include this package as a top level import by copying the folder into the root level of your application.
"C:\Python27\Lib\site-packages" is already on your python path. So appending path is unnecessary. Remove:
import sys
sys.path.append("C:\Python27\Lib\site-packages")
sys.path.append("C:\Python27\Lib\site-packages\vtk")
Create a new folder called 'vtk\' in "C:\Python27\Lib\site-packages", then create a new python file named __init__.py in "C:\Python27\Lib\site-packages\vtk" and put your own module vtk.py in this directory.
Using:
import vtk
or
from vtk import vtk
to use your own module.

Categories