Any way to include based on Python version - python

I need to detect Python version and execute code just for this version.
I want to use one file for each version of Python.

Use sys.version_info.
import sys
PY3 = sys.version_info[0] == 3
if PY3:
# execute code for python 3
import file_you_want_for_py3
else:
# execute code for python 2
import file_you_want_for_py2

If you're not sure which version of a module might be available, you can use a try/except:
try:
import new_shiny_module as module
except ImportError:
import old_rusty_module as module

the best and easiest way is to use tox like python library.

Related

Run Python module in another code but with an older version

I am using Python 3.10 but I want to run a Python module with Python 3.6 and use the result inside my code(that written with Python 3.10)
Please help me to solve it
thanks
Create a .py script that should be run with python3.6
Make the script prints your expected result
# script.py
import sys
print(sys.version[:6])
Use subprocess to get it
import subprocess
python3_6_result = subprocess.check_output(["python3.6", "script.py"])
print(python3_6_result) # 3.6.15

How to make an import statement work on both python 2 and python 3

I'm doing a migration for a code from python 2 to python 3.
there is some code that I'm not migrating and is necessary to the code that i do migrating,
therefor I need that some of the import statements work on both versions but the package got its name changed for example:
import urlparse # Python2
import urllib.parse as urlparse # Python 3
how can i code on statement that will work on both versions.
keep in mind that this question is for the general case (the example above is only one of the problems created by the following migration)
For your imports, you can do the following:
import sys
if sys.version_info[0] < 3:
#import your py2 packages
else:
#import your py3 packages

Python error, Can't import from stdlib error

I'm getting at error when trying to import from qtgui site-package. Here's an excerp from the code that's having and error.
from gnuradio import qtgui
on of the files in sitepackage qtgui is textparser which contains the statement:
from stdlib import read_file
That's where the error is thrown.
Following the traceback the problem is that on of the files in qtgui, called textparser.py tries to import read_file from stdlib.
Here is the traceback of the offending line.
File "/usr/lib/python2.7/site-packages/qtgui/textparser.py", line 2, in <module>
from stdlib import read_file
ImportError: No module named stdlib
The line "from stdlib import read_file" looks correct except i cant find any information about a package called stdlib for python. However stdlib sounds like the c library. I'm new to Python, is that the proper way to import a c library?
So am I missing a python package or module, or a c library?
By the way I'm using python2.7, and Centos 7. I can't use Python 3 because I'm using GNUradio which i don't think supports Python 3.
Thanks in advance.
there is no such library in stdlib in python there is a library called stdlib_list what you gonna supposed to do in your code put a code sample

How to add implicit imports to ipython?

I am using a the PYTHONSTARTUP=~/pythonstartup.py approach in order to make ipython the default python shell but I want to extend it do have some additional imports by default (like importing few common packages).
How can I do that?
#!/usr/bin/env python
import os
import sys
import re
import time
import platform
import IPython
os.environ['PYTHONSTARTUP'] = '' # Prevent running this again
IPython.start_ipython()
raise SystemExit
Problem solved by adding
IPython.start_ipython(user_ns=locals())

web2py external libraries

how can i import other external libraries in web2py? is it possible to
load up libs in the static file?
can somebody give me an example?
thanks
peter
If the library is shipped with python, then you can just use import as you would do in regular python script. You can place your import statements into your models, controllers and views, as well as your own python modules (stored in modules folder). For example, I often use traceback module to log stack traces in my controllers:
import traceback
def myaction():
try:
...
except Exception as exc:
logging.error(traceback.format_exc())
return dict(error=str(exc))
If the library is not shipped with python (for example, pyodbc), then you will have to install that library (using distutils or easy_install or pip) so that python can find it and run web2py from the source code: python web2py.py. Then you will be able to use regular import statement as described above. Before you do this make sure you installed the library properly: run python interpreter and type "import library_name". If you don't get any errors you are good to go.
If you have a third party python module or package, you can place it to modules folder and import it as shown below:
mymodule = local_import('module_name')
You can also force web2py to reload the module every time local_import is executed by setting reload option:
mymodule = local_import('module_name', reload=True)
See http://web2py.com/book/default/section/4/18?search=site-packages for more information.
In web2py you import external library as you normally do in Python
import module_name
or
from module_name import object_name
I am not sure what you mean by "in the static file"

Categories

Resources