Unittest: filename with point not loaded properly - python-3.x

Setup: PyCharm, Python 3.10
We have the naming convention to name our python unittest files like an URL. For example: my.domain.org.py
In the past, this was no issue. Now after an IDE and Python Update it does not run anymore. Selecting right click -> Run "Python tests in my.domain.org.py" throws the error:
Traceback (most recent call last):
File "D:\programs\Python\3.10.2\lib\unittest\loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'my'
It seems, the loader is interpreting the "." in the filename as path.
How can I run the unittest without renaming the file (which solves the issue)?

you cannot import python files with invalid names (in your case has dots in it) directly, but there's a turn around, you can use the imp library like (here in the example I have a function named print_smth that prints "it works!" in the my.file.py):
import imp
with open('my.file.py', 'rb') as fp:
my_file = imp.load_module(
'my_file', fp, 'my.file.py',
('.py', 'rb', imp.PY_SOURCE)
)
if __name__ == "__main__":
my_file.print_smth()
output:
test.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
it works!
P.S: preferably DO NOT DO THAT! it is highly deprecated!

Related

Python Relative Imports but I copied the documentation

I read all the other SO posts about this and it either doesn't work or uses sys.path.append.
Below is a replica of the official documentation:
All other files not shown are empty
moduleA.py
from ..subB.moduleB import MyClass
moduleB.py
class MyClass:
def __init__(self):
pass
package/subB/__init__.py
from .moduleB import MyClass
Traceback from running moduleA.py
Traceback (most recent call last):
File "path\to\my\projects\folder\package\subA\moduleA.py", line 1, in <module>
from ..subB.moduleB import MyClass
ImportError: attempted relative import with no known parent package
File Structure
The Python docs unfortunately forget to mention that their example only works if you run your code in a very specific way using the '-m' switch. So you would have to do python -m subA.moduleA and you need to ensure that your current working directory is package. Otherwise it will fail.
If you don't like these restrictions (like me), I've created an experimental import library: ultraimport
It gives you more control over your imports and lets you do file system based imports.
In moduleA.py you could then write:
import ultraimport
MyClass = ultraimport('__dir__/../subB/moduleB.py', 'MyClass')
This will always work, no matter how you run your code or what is your current working directory. Also no need to change sys.path. It will actually go to the file system and load the very file you've specified.

cant run file with sys.argv

I'm unable to go ahead. please help.
this is a link for the code i used
i tried downloading the module using cmd windows 10 but that is also showing an error...
C:\Users\kayza\Desktop\image_converter>python jpgtopng.py jpeg png
Traceback (most recent call last):
File "jpgtopng.py", line 5, in
image_input = sys.argv1
NameError: name 'sys' is not defined
The problem is that you import every module from the package sys. Because of that you can't access them with the dot notation (sys.argv[]).
You have two options to fix this.
Access the method like this argv[] without the sys. in front of it.
(the cleaner option)
Change your import statement to import sys

Cython: 'PyxImporter' object has no attribute 'find_spec'

I'm trying to integrate a Cython module into my project and I'm having trouble getting it to compile correctly. I have traced my problem to this minimal example:
Say I have two files a.py and b.pyx located in the same directory, if I then do the following in a.py:
import pyximport; pyximport.install()
import b
Then everything works fine, b.pyx is compiled and imported successfully.
But if I instead do this in a.py, assuming that a.py and b.pyx are located in dir1/dir2:
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
import pyximport; pyximport.install()
import dir1.dir2.b
(which is somewhat nonsensical in this example but illustrates what's preventing me from importing a from elsewhere in my module hierarchy), I get the following exception:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 888, in _find_spec
AttributeError: 'PyxImporter' object has no attribute 'find_spec'
Is this expected behaviour or is something wrong with my installation? (I'm on Windows 10)
NOTE: there is another question with a very similar title on SO which does not answer my question at all.
I had this error message (though not necessarily the same path situation) and it was resolved by upgrading Cython from 0.28.5 to 0.29.

Zipline import error. No module named zipline.transforms

I am not able to import the zipline.transforms module
>>> from zipline.transforms import batch_transform
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'zipline.transforms'
Disclaimer: I'm currently a maintainer of Zipline.
I'm guessing the reason you're seeing this error is because that particular module was removed a while back (assuming you're using zipline 1.0.0 or later). If you want to do things similar to transforms you'll need to call data.history() to get your pricing data, and call numpy/pandas functions like .avg() or .std(), or use talib.
I think you should firstly print out your sys.path (print sys.path), and then see where you zipline module is installed (somewhere like .../lib/python2.7/site-packages/zipline). Usually "no module named XXX" is caused by you sys.path doesn't contain the path you installed zipline. You should just add your zipline path into sys.path. Also use anaconda is good for zipline (http://www.zipline.io/install.html), so as to keep the environment tidy and clean.

python 3 and the file object

I am looking at using setup.py to automatically generate python 3 code from python 2 sources using the 'use_2to3' attribute. My setup.py script includes the following statement:
VERSION = None
with file('version','rt') as FF:
VERSION = FF.read().lstrip().rstrip()
print("VERSION %s" % (VERSION) )
When I type 'python3 setup.py build' I get the error:
Traceback (most recent call last):
File "setup.py", line 18, in <module>
with file('version','rt') as FF:
NameError: name 'file' is not defined
which I understand is correct as the file object no longer exists and I should change it to 'open()'.
The concern is that the '2to3' utility does not detect this and leaves the code untouched.
I unfortunately use this idiom throughout my code.
Is this a bug in '2to3' ?
Use the open instead of the file.
It was tempting to use the file() instead of the open() in Python 2.x -- especially for those with OO background. The file() call resembled calling a constructor for creation of a file object. However, it was always recommended to use open() function instead. This may be the reason why 2to3 does not solve the case.
In Python 3, the file is unknown. The file objects are of the _io.TextIOWrapper class:
>>> f = open('a.txt', 'w')
>>> type(f)
<class '_io.TextIOWrapper'>
>>> f.__class__.__name__
'TextIOWrapper'

Resources