I am working to incorporate the symspellpy package for spell checking and correcting large amounts of data. However, the package suggests using pkg_resources.resource_filename, which is no longer supported. Can you please provide guidance on how to access the necessary resources using the currently preferred method?
dictionary_path = pkg_resources.resource_filename("symspellpy", "frequency_dictionary_en_82_765.txt")
bigram_path = pkg_resources.resource_filename("symspellpy", "frequency_bigramdictionary_en_243_342.txt")
The replacement is the importlib_resources.files function.
It is integrated in standard library from Python 3.9, as importlib.resources.files
If you just need to support Python 3.9 or newer, it's straightforward
import importlib.resources
importlib.resources.files(...)
Otherwise, if you want to support Python 3.8 and older, here's how you do it:
add importlib_resources>=1.3; python_version < '3.9' to your dependencies (requirements.txt, setup.cfg, setup.py or pyproject.toml, depending how the project is organised)
In your code, adapt as
import sys
if sys.version_info >= (3, 9):
import importlib.resources as importlib_resources
else:
import importlib_resources
importlib_resources.files(...)
See https://importlib-resources.readthedocs.io/en/latest/migration.html
Related
Is there a way to specify minimum python version requirement for my script ? For example my script requires python 3.6+ because it uses f-string. I have test my script under 3.7, 3.8, 3.9, they all work.
But since pipfile doesn't support minimum version, refer to pipenv specify minimum version of python in pipfile? and this open issue https://github.com/pypa/pipfile/issues/87
So is there a way to do that ? Now I just write it in readme.
--- update ---
https://github.com/pypa/pipenv/issues/2683 indeed said
Note that the python_version key is optional. You can always remove it
if it causes problems, and document the version requirements otherwise
(i.e. in the README).
Check this post. This may do what you require:
#!/usr/bin/env python
import sys
if sys.version_info[0] != 3 or sys.version_info[1] < 6:
print("This script requires Python version 3.6")
sys.exit(1)
# rest of script, including real initial imports, here
I need to support both python 3.8 and versions lower that 3.8, but the package I need to import into my stub (*.pyi) file had different name in <3.8
import sys
if sys.version_info.minor < 8:
import xyz
else:
import zyx
In general this should do the job, but when I run flake8 with *.pyi files config (flake8 --config flake8-pyi.ini) I get this:
Y002 If test must be a simple comparison against sys.platform or sys.version_info
Any ideas what could be done about that?
Thanks in advance!
From the description of flake8-pyi (a flake8 plugin, not part of flake8 itself):
Y002: If test must be a simple comparison against sys.platform or sys.version_info. Stub files support simple conditionals to indicate differences between Python versions or platforms, but type checkers only understand a limited subset of Python syntax, and this warning triggers on conditionals that type checkers will probably not understand.
The fix is to change your condition to:
if sys.version_info < (3, 8):
note that your code would break for 2.8 (yes, some people do this!) and 4.0 so you should be careful with eliding parts of comparisons ;) -- I've written a flake8 plugin which helps lint against conditions that might be problematic: flake8-2020
Iam new bee to the jmeter
My code is working in the Python 2.7 with importing additional packages Dateutil, parser .
Problme : But when I am trying to run same code in the J Meter-JSR-223 PreProcessors , an error saying No module named dateutil in.
So , I have tried another approach to use Jython .
Installed the Jython ( downloaded the dateutil) and provide the packages reference under
import sys
sys.path.append('C:/Jython27/Lib/site-packages')
sys.path.append('C:/Jython27/Lib/site-packages/python_dateutil-2.4.2-py2.7/dateutil')
sys.path.append('C:/Jython27/Lib/site-packages/python_dateutil-2.4.2-py2.7/dateutil')
Now packages error is gone but string syntax error is present .
java.sql.Date' object has no attribute .
I believe dateutil package can be picked up from CPython as it doesn't require any extra wrappers for Java.
Install dateutil normally using pip like:
pip install python-dateutil
Add site-packages folder of Python (not Jython) installation to sys.path like:
sys.path.append("C:\Python27\Lib\site-packages")
That's it, now you should be able to use dateutil module functions from the JSR223 Test Elements:
Be aware that invoking Python scripts via Jython interpreter is not the best idea from performance perspective and if you're about to invoke your Python code only limited number of times and/or with a single thread - it might be better to go for the OS Process Sampler.
If you plan to use the Python code to create the main load - consider using Locust tool instead of JMeter. If you don't want to change JMeter a good approach would be rewriting your Python code in Groovy - it will be way better from the performance perspective.
hi please find follwing
import sys
sys.path.append('C:/Python27/Lib/site-packages')
sys.path.append('C:/Python27/Lib/site-packages/python_dateutil-2.4.2-py2.7/dateutil')
from dateutil.parser import *
sourceDateTimeOfEvent = ""
dateTimeOfEvent = ""
a=parse('2016-07-01 13:00:00')
sourceDateTimeOfEvent = a.isoformat()+"+05:30Z"
dateTimeOfEvent = a.isoformat()+ "Z"
vars.put("sourceDateTimeOfEvent", sourceDateTimeOfEvent)
vars.put("dateTimeOfEvent", dateTimeOfEvent)
This sourceDateTimeOfEvent and dateTimeOfEvent considered as two variables and passed it to the json file
I created a python library which depends on pypiwin32 package. For some functionality, they use _winreg package. It works on Windows, however RTD virtualenv is not running on Windows and this package is not available. Since it is part of python itself and not on pypi, there is no way I could make it a dependency.
Each time i build docs from source code, it fails on missing the _winreg package.
I tried to remove dependency on pypiwin32 only for RTD with something like this in setup.py:
if os.environ.get('READTHEDOCS') == 'True':
REQUIRED = []
else:
REQUIRED = [
"pypiwin32"
]
It works for all .rst Sphinx files. On the other hand, no documentation for functions is generated. On local machine (Windows) everything is properly documented.
Note: Read the docs documentation is generated from rtd branch of my github project.
Is there any workaround for this?
Thank you.
I might have found working solution. Since Read The Docs does not support _winreg, I disabled whole dependency on pypiwin32 for Read The Docs.
# ... part of setup.py
if os.environ.get('READTHEDOCS') == 'True':
REQUIRED = []
else:
REQUIRED = [
"pypiwin32"
]
READTHEDOCS is environmental variable available only on RTD, see more.
This broke all calls from my library. Thus I created another file with mockup functions just for Read The Docs:
# ... part of __init__.py
if os.environ.get('READTHEDOCS') != 'True':
from win32api import GetModuleHandle
# ... import rest of win32api functions
else:
from .read_the_docs import *
And content of read_the_docs.py:
def GetMOduleHandle(*args, **kwargs):
pass
# ... rest of file
This way, local build use full pypiwin32 with _winreg, but on Read The Docs, Sphinx uses this kind of "mockup" utility.
With my Java projects at present, I have full version control by declaring it as a Maven project. However I now have a Python project that I'm about to tag 0.2.0 which has no version control. Therefore should I come accross this code at a later date, I won't no what version it is.
How do I add version control to a Python project, in the same way Maven does it for Java?
First, maven is a build tool and has nothing to do with version control. You don't need a build tool with Python -- there's nothing to "build".
Some folks like to create .egg files for distribution. It's as close to a "build" as you get with Python. This is a simple setup.py file.
You can use SVN keyword replacement in your source like this. Remember to enable keyword replacement for the modules that will have this.
__version__ = "$Revision$"
That will assure that the version or revision strings are forced into your source by SVN.
You should also include version keywords in your setup.py file.
Create a distutils setup.py file. This is the Python equivalent to maven pom.xml, it looks something like this:
from distutils.core import setup
setup(name='foo',
version='1.0',
py_modules=['foo'],
)
If you want dependency management like maven, take a look at setuptools.
Ants's answer is correct, but I would like to add that your modules can define a __version__ variable, according to PEP 8, which can be populated manually or via Subversion or CVS, e.g. if you have a module thingy, with a file thingy/__init__.py:
___version___ = '0.2.0'
You can then import this version in setup.py:
from distutils.core import setup
import thingy
setup(name='thingy',
version=thingy.__version__,
py_modules=['thingy'],
)