How do tell "swig -python -py3 myswig.i" not to include annotations - python-3.x

I need to use SWIG to support both Python 2.7 and Python 3.10. [Yes, I know that Python 2.7 is dead, and we're doing our best to migrate users away from it as quickly as we can.]
I generate my module via setuptools.setup with a Swig extension. I can run the setup using both Python2 and Python3. The setuptools program creates a separate shareable library for the Python2 and Python3 runs. However both runs generate a myswig.py file in the same location.
It turns out that the Py2 and Py3 generated files are identical, except that the Py3 generated file contains annotations for the functions and the Py2 doesn't. Python3 can read the Python2 generated file and works just fine. Python2 cannot read the Python3 generated file.
I've tried both %feature("autodoc", 0); and leaving out this line completely, and I still get annotations.
So is there someway of either:
Turning off annotations in the generated file
Adding from __future__ import annotations automatically to the generated file

Don't use -py3. It's not required for Python 3, but enables Python 3-specific code features like annotations.

Related

Where can i find the pyc file?

The python3 version is Python 3.5.3 in my os.
mkdir workspace
cd workspace
vim print.py
print("i am learning")
Saved and exit.
python3 print.py
i am learning
As far as i knew, python source file was parsed and compiled into pyc file when to execute it.
ls
print.py
There is no pyc file in workspace directory,where is the complied print.py file then?
sudo find / -name ".pyc"
The find command still can't search pyc file such as print.pyc .
python3 -m compileall can create the compiled file for print.py manually,where is the compiled file for print.py created by python itself?
Does python3 delete the print.pyc after executing python3 print.py?
Ok this is one big of a problem I ever had when I'm started to learn python few years back. Python is just like any other oop programming languages which does compilation before program execution. When python compiles its program, it creates the bite code which is you can see by standard library called dis.
import dis
print(dis.dis(your_program))
Sometimes (not always) python creates .pyc file for the running programs to improve the speed up the loading of import modules but not to improve the execution time. So hope you get intuition behind .pyc, furthermore .pyc only creates when your module is import by another module.
As an example, Imagine you have this print.py (Let's modify it shall we)
def return_print_statment(statement):
print('Printed version: ', statement)
Suppose this module imported by another custom module called views.py. In views.py there is a module_view which will use the return_print_statment
from print import return_print_statment
def module_view():
...
return_print_statment(output)
So in the compilation, since you have imported the print.py python will generate print.pyc file for it. In python 2.0 python will put the .pyc to right next to your program in the same folder, but in python3 instead of creating in the same folder python will create separate folder called __pycache__ in the same directory to put these byte codes.
python3 -m compileall .
To compile all .py files in your current directory.
http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm

Using a python library (Biopython) from a python program on a different folder that is installed

I generally like to make my python programs in a text editor and then run them after they are complete instead of line arguments. Thus, I save those .py files on a convenient folder location instead of Python program files.
I then run my .py file using Command Prompt. However it has not worked for the Biopython library as import Bio gives back a Traceback No module named 'Bio'. However, using line arguments directly on python shows it is installed.
I have never had this issue with Python in general and other downloaded libraries (import numpy for example works fine). How do I make the files available to be open from any location?? Or how do I provide the path in import?
To clarify and add:
1) I use Python from Windows 10
2) I downloaded and installed Python(3.7) from python.com
3) I downloaded Biopython using pip (and all other libraries I downloaded)
4) I also tried it on Jupyter notebook and also does not work to import Bio, whereas Import numpy does.
Thanks!

How can I list all non-standard modules used by a Python program?

As some pundits say that the Python standard library is listed in the Python core documentation for your version, it is built-in by default, you don't have to install it separately from Python itself. For example, math is a standard module, you needn't install it with pip install math.
Non-standard modules are not built-in, you have to install them before you use them in a Python program. For example, lxml is a non-standard module. If not installed, a "no module named lxml" error pops up when you import lxml in the Python shell.
It is time to turn to the topic now, how can I list all non-standard modules used by a Python program?
test.py is a Python program, it is executed with python test.py. How many non-standard modules are called when python test.py is run?
pip freeze shows all installed modules, but some of them are not called by python test.py.
I was wondering the same thing, because I wanted to set up a conda environment for my project by a method other than trial-and-error. What I found worked was to make a bare environment with just python and pylint in it, using, e.g.,
conda create -n myenv python=3 pylint
and then from within that environment run:
pylint /path/to/module --disable=all --enable=import-error
This will nicely list, by file, all of the non-standard imports.

How to call 2to3 before run py2app

I have a setup.py that use py2app, and I want to run 2to3 to convert python script to Python 3 compatible before build the app. I used option setup(use_2to3=True), but it did not call 2to3. So now I use a Makefile to work around this problem. Any pythonic solution? The setup.py is below. Please help.
import sys
from setuptools import setup
from plistlib import Plist
plist = Plist.fromFile('Info.plist')
OPTIONS = {
'iconfile': 'python.icns',
'plist': plist
}
if sys.version_info.major < 3:
app = "PyInterpreter.py"
else:
app = "build/PyInterpreter.py"
setup(
name="PyInterpreter",
app=[app],
data_files=["English.lproj"],
options={'py2app': OPTIONS},
setup_requires=["py2app"],
use_2to3=True,
)
Thanks.
py2app does not support use_2to3, and likely never will unless someone contributes a patch (I'm the maintainer of py2app).
The cleanest solution to use 2to3 is to call it yourself, for example in a custom distutils command that subclasses py2app.build_app.py2app (implement a run method that calls 2to3, possibly adjust the build environment, then call the py2app.run method).
It is often much nicer to not use 2to3, but convert the code to something that runs with both python 3 and python 2. That's fairly easy when you can drop support for python 2.5 (and more so when you only need to support 2.7 or later), as most of the syntax of python 3 is supported in 2.6.
BTW. plistlib.Plist.fromFile is deprecated, use plistlib.readPlist instead.

Mako: cannot import the Template class. Have a SyntaxError error in "\mako\template.py", line 622

I want to try Mako with Django instead of Django's default template language. But I'm having a problem when I try to import Mako's Template class as written in the manual:
from mako.template import Template
mytemplate = Template("hello world!")
print mytemplate.render()
I do this in Windows cmd and receive such an error:
C:\Documents and Settings\User>cd C:\py\project\vendor\template\Mako_73 // cd to where I unpacked Mako
C:\py\project\vendor\template\Mako_73>python // run Python interpreter
>>> from mako.template import Template // trying to import and getting an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".\mako\template.py", line 622
exec code in module.__dict__, module.__dict__
^
SyntaxError: invalid syntax
The code from that part:
def _compile_text(template, text, filename):
identifier = template.module_id
source, lexer = _compile(template, text, filename,
generate_magic_comment=template.disable_unicode)
cid = identifier
if not util.py3k and isinstance(cid, unicode):
cid = cid.encode()
module = types.ModuleType(cid)
code = compile(source, cid, 'exec')
exec code in module.__dict__, module.__dict__
return (source, module)
What can it be? I couldn't find anything in Google about this error.
I'm using Python 3.3.
I've downloaded Mako-0.7.3 as tar.gz file and just unzipped it in
C:\py\poject\vendor\template\Mako_73. I do not have this directory in the PYTHONPATH or paths.pth. C:\py\poject is a directory where my Django project lives and in \vendor\template I've decided to put Mako and import it from there.
UPD
I found the solution. I've installed the Pyramid Framework and have taken the Mako from there as the Mako is a default template system in it. And Pyramid's version works fine.
Your basic problem is that you are using Python 3, which is relatively new for large projects like Django.
Secondly, you need to find out how to install packages correctly. I don't know where you got Mako from, but you won't find anywhere that says "just unpack the tarball" - perhaps you are assuming that from your knowledge of PHP, but it's not correct.
On the Mako site, the suggested method of installation is pip.
If you go for downloading manually, you need to read instructions about installing Python packages, for example here: http://wiki.python.org/moin/CheeseShopTutorial
The reason Mako doesn't work for you is that the installation procedure (which you haven't run) converts all the provided Python 2 code so that it works on Python 3. It is not that someone didn't bother to check the code for basic syntax errors!
If you are trying to use Django, though, Python 3 is definitely the wrong choice - the installation instructions clearly say you need to be using Python 2.5 to 2.7: https://docs.djangoproject.com/en/1.4/intro/install/
Since you are new to Python, you should try to walk before you run, and go with the tried and tested path - which is Python 2.7 for Django. Ignoring installation instructions and requirements will only slow you down and make life hard.

Resources