I was trying to create a class in python with 'RawIOBase' as given below.
try:
import io
except ImportError:
class Serial(PosixSerial, FileLike):
pass
else:
class Serial(PosixSerial, io.RawIOBase):
pass
I was trying to run this using Python 2.6, but it is displaying the error:
AttributeError: 'module' object has no attribute 'RawIOBase'
I found the reason for this error is one more directory with name 'io' exists and 'init.py' file exists in the directory. so when I print using print(io), it is displaying
<module 'io' from '/projects/phx/tools/io/__init__.pyc'>.
And I found that
<module 'io' from '/usr/lib/python2.6/io.pyc'>
should be the path. But io.py file doesn't exist in /usr/lib/python2.6 path. So even though I exported PYHTONPATH to /usr/lib/python2.6, still when I import io, it is referring the local one instead of standard io module.
Can you please let me know where can I find the io.py file so that this program will be working.
Try this to see what you are using right now:
import io
print io.__file__
# /usr/lib/python2.7/io.pyc
the .py file should be in the same directory
You can use also use locate in shell to find all io.py versions:
/usr/lib/python2.7/io.py
/usr/lib/python2.7/io.pyc
/usr/lib/python2.7/dist-packages/IPython/utils/io.py
/usr/lib/python2.7/dist-packages/IPython/utils/io.pyc
# etc...
Keep in mind, that it might depend on how are you executing python, using different python version or - for example - IPython, can yield different results.
Related
import winshell
r = list(winshell.recycle_bin())
for index, value in enumerate(r):
print(index, value.original_filename())
This is the simple script I wrote, but when I try running it (or antyhing else that uses winshell) I get this error:
ModuleNotFoundError: No module named 'win32'
And when I try running pip install win32 I get another error:
ERROR: Could not find a version that satisfies the requirement win32 (from versions: none)
ERROR: No matching distribution found for win32
So now I'm even more confused. Why does winshell need a different module? That module doesn't even exist. Is it fine if I use some different module than the non-existent win32? If so which one? What am I supposed to do now?
First, you have to execute the script inside the Scripts directory, the pywin32_postinstall.py. Let’s say your Python directory is C:\python3, just follow the code below.
cd C:\python3
python Scripts/pywin32_postinstall.py -install
After that, the installation will drop the DLL files under the C:\Windows\System32. You need to move those two files ( pythoncom310.dll and pywintypes310.dll) to C:\python3\Lib\site-packages\win32 directory.
After that, you need to edit the python310._pth that you can find inside the Python installation folder. Then make the following changes:
Lib/site-packages
Lib/site-packages/win32
Lib/site-packages/win32/lib
Lib/site-packages/pythonwin
python310.zip
#Uncomment to run site.main() automatically
#import site
Save and try running your code again.
Troubleshoot
If you still get an error saying “ImportError: DLL load failed while importing win32api: The specified module could not be found.”, make sure you have copied the two dll files to Lib\site-packages\win32 directory.
PythonWin32Api
I am trying to import modules while running my main python script, using a smaller setup.py script. However the importlib command: importlib.util.spec_from_file_location(name, location) doesn't appear to be detecting my small python script. Presumably I'm not filling in the name or location fields correctly.
Example Script A (setup.py):
import os
import pandas as pd
print("success!") # So I can see it has run.
Example Script B (my_script.py):
import importlib
setup_path = ("/home/solebay/My Project Name/")
start_up_script = importlib.util.spec_from_file_location("setup.py", setup_path)
module = importlib.util.module_from_spec(start_up_script)
Running the above snippet returns:
AttributeError: 'NoneType' object has no attribute 'loader'
I subsequently investigated by running type(start_up_script) the result it gives is typeNone.
The paths are correct. I verified this by running the following:
"/home/solebay/My Project Name/"
sudo python3 "/home/solebay/My Project Name/setup.py"
These printed the messages is a directory and success! respectively.
Note: Maurice Meyer succeeded in answering my main question, and so I have marked it as correct. However, I have not achieved my main goal; namely importing modules via another script. So if that is your aim, this question might not be appropriate for you.
The location argument passed to spec_from_file_location has to be the full path to the python script:
import importlib.util
spec = importlib.util.spec_from_file_location(
name='something__else', # name is not related to the file, it's the module name!
location='/tmp/solebay/My Project Name/setup.py' # full path to the script
)
my_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(my_mod)
print(my_mod)
Out:
success!
<module 'something__else' from '/tmp/solebay/My Project Name/setup.py'>
According to the python doc the -m flag should do the following:
Search sys.path for the named module and execute its contents as the
__main__ module.
When I run my script simply with the python command, everything works fine. Since I now want to import something from a higher level, I have to run the script with python -m. However the __name__ == "__main__" statement seems to return False and the following Error is produced:
/home/<name>/anaconda3/bin/python: Error while finding module specification for 'data.generate_dummies.py' (AttributeError: module 'data.generate_dummies' has no attribute '__path__')
I dont't understand what the __path__ attribute has to do with that.
The error you get occurs when python tries to look for a package/module that does not exist. As user2357112 mentions, data.generate_dummies.py is treated as a fully specified module path (that does not exist), and an attempt is made to import a submodule py (that is also non-existent).
Invoke your file without .py, if you're using the -m flag, like this:
python -m data.generate_dummies
This is my first project in Python and I have just learnt the unittest framework. The test module runs well when I do python test_module.py but when I want to execute a certain class or a method as said in the documentation using:
python -m unittest test_module.TestClass.test_method # or even just test_module
I get the following error:
AttributeError: 'module' object has no attribute 'test_module'
The directory where I run the command contains graphm_test.py (I also tried to change the name to test_graphm.py), with class graphm_test(unittest.TestCase): and methods all starting with test_* and here is the command I run on the terminal:
python -m unittest test_graphm.py
I could not find a similar problem to this anywhere, it would be great to know the reason behind the error and how to run a certain class inside the module or a certain method
There are 2 issues: you are not calling the correct module name, and you are using the extension .py.
So you need to be in the folder with your graphm_test.py file, and run:
python -m unittest graphm_test
This concerns the importing of my own python modules in a HTCondor job.
Suppose 'mymodule.py' is the module I want to import, and is saved in directory called a XDIR.
In another directory called YDIR, I have written a file called xImport.py:
#!/usr/bin/env python
import os
import sys
print sys.path
import numpy
import mymodule
and a condor submit file:
executable = xImport.py
getenv = True
universe = Vanilla
output = xImport.out
error = xImport.error
log = xImport.log
queue 1
The result of submitting this is that, in xImport.out, the sys.path is printed out, showing XDIR. But in xImport.error, there is an ImporError saying 'No module named mymodule'. So it seems that the path to mymodule is in sys.path, but python does not find it. I'd also like to mention that error message says that the ImportError originates from the file
/mnt/novowhatsit/YDIR/xImport.py
and not YDIR/xImport.py.
How can I edit the above files to import mymodule.py?
When condor runs your process, it creates a directory on that machine (usually on a local hard drive). It sets that as the working directory. That's probably the issue you are seeing. If XDIR is local to the machine where you are running condor_submit, then it's contents don't exist on the remote machine where the xImport.py is running.
Try using the .submit feature transfer_input_files mechanism (see http://research.cs.wisc.edu/htcondor/manual/v7.6/2_5Submitting_Job.html) to copy the mymodule.py to the remote machines.