what does "__requires__" mean in python? - python

I am newbie in python.
Could anybody answer what does __requires__ means in the following code?
Why would they put __requires__ = 'flower==0.4.0' in the beginning of the file?
#!/srv/virtualenvs/zeusenv/bin/python
__requires__ = 'flower==0.4.0'
import sys
from pkg_resources import load_entry_point
sys.exit(
load_entry_point('flower==0.4.0', 'console_scripts', 'flower')()
)

The __requires__ line is part of a generated console script. It has no meaning to Python itself, only the setuptools library uses this information.
Console scripts are python scripts defined in a python package metadata, and setuptools installs wrapper script files to let you run them as command line scripts. The flower file installed in your virtualenv is such a script, defined by the flower package setup.py file.
The pkg_resources module imported in the wrapper script inspects the value of __requires__ in the main script to make sure the correct version of the library is available and loaded before the load_entry_point function (or any other pkg_resources function) is run. It'll not install the version specified, it is assumed that that version is already installed on your system. It's purpose is to avoid loading incorrect, incompatible resources when the script runs and loads dependencies.

Related

ImportError: No module named _analog_swig

I am having issues getting python to import the _analog_swig gnuradio module in order to run gnuradio code on a Windows 8.1 64bit machine.
Some background: I am running Python 2.7.10 (installed in C:\Python27) and have installed the latest gnuradio binary (v3.7.11.1/v1.3 64-Bit Any CPU) from here: http://www.gcndevelopment.com/gnuradio/downloads.htm. I have installed gnuradio to C:\Program Files\GNURadio-3.7 .
I can run gnuradio companion and run flowgraphs from GRC successfully (which calls "C:\Program Files\GNURadio-3.7\bin\run_gr.bat" gnuradio-companion.py).
I have added & verified the following system variables are set:
Path: C:\Program Files\GNURadio-3.7\bin
PYTHONPATH: C:\Program Files\GNURadio-3.7\lib\site-packages
GRC_BLOCKS_PATH: C:\Program Files\GNURadio-3.7\share\gnuradio\grc\blocks
Now to the problem: If I run e.g. CMD and type:
python C:\test\top_block.py
I am returned the following ImportError:
File "C:\test\top_block.py", line 22, in <module>
from gnuradio import analog
File "C:\Program Files\GNURadio-3.7\lib\site-packages\gnuradio\analog\__init__.py", line 33, in <module>
from analog_swig import *
File "C:\Program Files\GNURadio-3.7\lib\site-packages\gnuradio\analog\analog_swig.py", line 17, in <module>
_analog_swig = swig_import_helper()
File "C:\Program Files\GNURadio-3.7\lib\site-packages\gnuradio\analog\analog_swig.py", line 16, in swig_import_helper
return importlib.import_module('_analog_swig')
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named _analog_swig
The folder content of C:\Program Files\GNURadio-3.7\lib\site-packages\gnuradio\analog is as follows:
Comparing this to the folder content on a linux machine, which has a working install of gnuradio that works with python as I want it:
The difference seems to be that the folder in windows contains only a _analog_swig.pyc file, whereas the folder in linux contains a _analog_swig_.so file.
Any idea why the _analog_swig module can apparently not be imported in windows?
My plan is to be able to run gnuradio code directly from my python interpreter and being able to create compiled gnuradio executables so any help on how this could be fixed is much appreciated.
I've been struggling with this for the past few days, but I finally figured it out. I was trying to run GnuRadio Companion generated code in IDLE and also in PyCharm. I kept failing miserably with this same error. I finally figured it out:
-As Flexo says, the PYD file (_analog_swig.pyd) is actually a Windows DLL. The error makes it sound like Python is not finding that file, but that is not at all what was happening. The PYD file, being a DLL, has dependencies itself. Python is able to find _analog_swig.pyd just fine, but it could not find the DEPENDENCIES of that library.
-To verify if that's what wrong in your installation, download and use DependencyWalker (Google it) to check if your system can find the dependencies to _analog_swig.pyd.
-The fix for me was to add the GnuRadio-3.7/bin folder to my PATH environment variable. Inside that folder are a number of DLLs that the _analog_swig.pyd library needs to load. If you don't have the folder in your PATH, the module will fail to load in Python and throw the error you see above.
-I see that you verified that this folder is in your PATH, so this is apparently not the same problem, although your symptoms are exactly the same as mine. i.e. the GRC code would run just fine when you start with "run_gr.bat", but not when you run from a normal CMD window.
Hopefully that helps someone else that wants to use GNURadio Python code on Windows.
Friend,
As you mentioned, the GNU Companion calls \bin\run_gr.bat gnuradio-companion.py. That batch script does quite a bit of work on windows environment variables (try opening it in a text editor if you're curious).
In a sense, the run_gr.bat script puts together a temporary, custom python workspace for gnuradio so it can import anything it needs. It receives python scripts to run in this environment as command line arguments; hence, you can use it to run any GNU radio python code you want in your windows command prompt. Generally, you would call
<gnuradio_install_path>\bin\run_gr.bat <gnu_radio_code>.py
To test your import, you can try
# test.py
from gnuradio import analog
try calling the following from the command prompt, in the test.py directory:
<gnuradio_install_path>\bin\run_gr.bat test.py

Import from my package recognized by PyCharm but not by command line

I've created the following package tree:
/main_package
/child_package
version.py
where version.py contains a single string variable (VERSION)
Inside my script in child package I'm importing version.py by the following line:
from main_package.version import VERSION
While I'm running the code from PyCharm everything works great, however when I'm running the code via the command line I'm getting the following error message:
C:\Users\usr\PycharmProjects\project\main_package\child_package>python script.py
Traceback (most recent call last):
File "script.py", line 2, in <module>
from main_package.version import VERSION
ModuleNotFoundError: No module named 'main_package'
I've found in the internet that I might need to add my package to the python path, however it doesn't seems to work for me
PyCharm sets the Python Path at the root of the project (by default). To mimic this in a quick'n'dirty fashion, you just need to do this once in your shell session before invoking python whatever:
set PYTHONPATH=C:\Users\usr\PycharmProjects\project
The pythonic way is to have a setup.py file to install your project in the system (check python Minimal Structure):
from setuptools import setup
setup(name='main_package',
version='0.1',
description='main package',
license='MIT',
packages=['main_package'],
zip_safe=False)
Then you install it as follow:
python setup.py install for global installation
OR
python setup.py develop for local installation in editable mode

py2exe "No module named 'site'" from Anaconda

I'm trying to use py2exe to build an executable on 64-bit Windows 7 with Anaconda (Python 3.4) for a project of mine that depends on a lot of libraries. Some of the more complex ones include vispy (pyopengl), PyQt4, numba, and scipy. I've been stepping through various errors to try to get a working executable, but have hit a road block with no clear way forward. Currently, the py2exe command completes, but I get the following error when running the exe:
...
from numba import jit
File "C:\Anaconda3\envs\sift_py2exe\lib\site-packages\numba\__init__.py", line
13, in <module>
from .pycc.decorators import export, exportmany
File "C:\Anaconda3\envs\sift_py2exe\lib\site-packages\numba\pycc\__init__.py",
line 12, in <module>
from .cc import CC
File "C:\Anaconda3\envs\sift_py2exe\lib\site-packages\numba\pycc\cc.py", line
4, in <module>
from distutils.command import build_ext
File "C:\Anaconda3\envs\sift_py2exe\lib\distutils\command\build_ext.py", line
17, in <module>
from site import USER_BASE
ImportError: No module named 'site'
I was able to do a small workaround by adding the C:\Anaconda3\envs\sift_py2exe\Lib directory to sys.path in my main script, but I doubt that's going to help me much later. Not to mention I had more scipy DLL issues after that.
Here are the relevant parts of my setup.py:
try:
import py2exe
from llvmlite.binding.ffi import _lib_dir, _lib_name
kwargs["data_files"] = [('.', [os.path.join(_lib_dir, _lib_name), os.path.join(_lib_dir, "MSVCP120.dll"), os.path.join(_lib_dir, "MSVCR120.dll")])]
kwargs["console"] = [{
'script': 'cspov/__main__.py',
'dest_base': "SIFT",
}]
kwargs["options"] = {'py2exe': {"includes": ["vispy.app.backends._pyqt4", "PyQt4.QtNetwork"]}}
except ImportError:
print("'py2exe' and/or 'llvmlite' not available")
I've tried adding the "Lib" directory in setup.py and then including "site", but it doesn't find the module. Any ideas? Thanks.
Side note: I'm using the Microsoft DLLs from llvmlite as a quick workaround because I couldn't get it to work in any of the normal ways.
This isn't the answer I was hoping for, but I was able to get a working executable when I switched to pyinstaller. All of the other SO questions I saw relevant to my problem had similar "solutions".

How does this python code work?

I have just started writing some simple scripts in python as I've started using ubuntu as my default operating system.
So I came across the code for mpsyt (terminal youtube player). I was surprised how simple the coding was. Could anybody explain what is going on here? I don't undestand how a seemingly complex program could have such a small amount of code....
#!/usr/bin/python3
# EASY-INSTALL-ENTRY-SCRIPT: 'mps-youtube==0.2.5','console_scripts','mpsyt'
__requires__ = 'mps-youtube==0.2.5'
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('mps-youtube==0.2.5', 'console_scripts', 'mpsyt')()
)
This is an autogenerated stub; it executes an entry point in the mps-youtube package installed in your site-packages. It is not the actual script.
The setuptools project installs such stubs in the bin directory; it's task is to find the right version of the project and load the real script.
There will be a matching lib/python[version]/mps-youtube-0.2.5-py[version].egg-info directory holding metadata, including an entry_points.txt file that contains INI-file-format style information on the package. The load_entry_point('mps-youtube==0.2.5', 'console_scripts', 'mpsyt') line will look for that exact file to load the mpsyt definition from the console_scripts section.
In the [console_scripts] section there will be a mpsyt entry that names the actual module used to run the script. Judging by the project setup.py file that'll look like this:
[console_scripts]
mpsyt = mps_youtube:main.main
pointing to the mps_youtube.main module, where the main() function will be called to do the actual work; look for a lib/python[version]/mps_youtube/main.py file, then search for a def main(): function definition. From Github you can see the actual script is a little longer.
This isn't the entire program. This just runs the rest of the program.

Compiling python script with a dependable python script using pyinstaller - QT related error

I need to make a python executable in ubuntu, of my code, which has got a python dependency of another python class.
dbscan.py
class dbscan:
#some code goes here
helloworld.py
import dbscan
from dbscan import *
#again some more code goes here
I am using pyinstaller to create the executable,
pyinstaller helloworld.py
If I use the same for a python program which doesn't uses classes from other python files it runs just fine, but when i create executable with a case like above I get this error, when i run the executable
File "<string>", line 36, in <module>
ImportError: No module named QtCore

Categories