Python external on Azure ImportError No Module Found - python

If i locally run my code with python main.py, then this error occurs locally. If i run it with python3 main.py, it works as expected. I suspect my azure app service is not running the correct version of python, however, it is set to python 3.4
StdErr:
2018-06-30 19:47:13.785205: Unhandled exception in wfastcgi.py: Traceback (most recent call last):
File "D:\Python34\Scripts\wfastcgi.py", line 711, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\Python34\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
return env, get_wsgi_handler(handler_name)
File "D:\Python34\Scripts\wfastcgi.py", line 541, in get_wsgi_handler
handler = handler()
File ".\ptvs_virtualenv_proxy.py", line 120, in get_venv_handler
handler = get_wsgi_handler(os.getenv('WSGI_ALT_VIRTUALENV_HANDLER'))
File ".\ptvs_virtualenv_proxy.py", line 89, in get_wsgi_handler
raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb))
ValueError: "main.app" could not be imported: Traceback (most recent call last):
File ".\ptvs_virtualenv_proxy.py", line 73, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File "D:\home\site\wwwroot\main.py", line 2, in <module>
from newspaper import Article
ImportError: No module named 'newspaper'
UPDATE
So I went to the debug console in kudo and i ran python
-m pip install --upgrade -r d:\home\site\wwwroot\requirements.txt.
Here is the error
ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" ** make sure the development packages of libxml2 and libxslt are installed ** Using build configuration of libxslt
One of the requirements is Pillow which requires libxml2 and libxslt. I've tried using wheels however, I've had no success.
UPDATE II
I tried to install virtualenv since it does not exist and I receive a permissions error.
UPDATE III
I ran this command
D:\home\python364x64>python -m pip install newspaper3k
I test everything locally, and it works perfect. I push and receive this error below.
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "D:\home\python364x64\wfastcgi.py", line 791, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\home\python364x64\wfastcgi.py", line 633, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "D:\home\python364x64\wfastcgi.py", line 600, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File ".\app.py", line 8, in <module>
from newspaper import Article
File "D:\home\python364x64\lib\site-packages\newspaper\__init__.py", line 10, in <module>
from .api import (build, build_article, fulltext, hot, languages,
File "D:\home\python364x64\lib\site-packages\newspaper\api.py", line 12, in <module>
import feedparser
File "D:\home\python364x64\lib\site-packages\feedparser.py", line 316
raise KeyError, "object doesn't have key 'category'"
^
SyntaxError: invalid syntax
StdOut:
StdErr:

Per my experience, we have no permission to modify D:\home environment.
So , It's better to use python extension. Please follow the steps in this case: Azure Flask Routes Not found and use command python -m pip install newspaper to install additional packages in python extension.
Please make sure the package you want to install must match with version of python extension. Otherwise, it will throw error as follows.
Hope it helps you.

Related

Pyinstaller can't find package importlib_metadata

I created a new poetry project.
main.py:
import importlib_metadata
PACKAGE_NAME = 'try_pyinstaller'
def get_version():
version = importlib_metadata.version(PACKAGE_NAME)
return version
def main():
version = get_version()
print(version)
if __name__ == "__main__":
main()
when I run pyinstaller with:
pyinstaller --onefile --name main try_pyinstaller\main.py --clean
i get the error:
C:\Users\micha\voltfang_software\tests\python\try_pyinstaller\dist>main.exe
Traceback (most recent call last):
File "importlib_metadata\__init__.py", line 381, in from_name
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "try_pyinstaller\main.py", line 14, in <module>
File "try_pyinstaller\main.py", line 10, in main
File "try_pyinstaller\main.py", line 6, in get_version
File "importlib_metadata\__init__.py", line 832, in version
File "importlib_metadata\__init__.py", line 805, in distribution
File "importlib_metadata\__init__.py", line 383, in from_name
importlib_metadata.PackageNotFoundError: No package metadata was found for try_pyinstaller
[2576] Failed to execute script 'main' due to unhandled exception!
The wierd thing is when I just change variable in version = importlib_metadata.version(PACKAGE_NAME) to version = importlib_metadata.version('try_pyinstaller') it works totally fine. Same environment, nothing else changed. Just put the hardcoded string in.
is this a bug or am I doing something wrong?
The answer to this question can be found here:
https://github.com/pyinstaller/pyinstaller/issues/7399
In short:
If you use importlib_metadata.version('try_pyinstaller'), PyInstaller's bytecode scanning can infer that metadata for try_pyinstaller needs to be collected. If you use a variant with non-literal argument, importlib_metadata.version(PACKAGE_NAME), it cannot do that, and you need to explicitly instruct it to collect metadata via --copy-metadata try_pyinstaller.

Running my Python script in my virtual env?

Total python noob here, I want to run my .py flask api file, i cd to my directory, activate my python virtual env, then i try run.py
Issue is that it prints:
$ python run.py
Traceback (most recent call last): File "run.py", line 1, in
from flask_sqlalchemy import SQLAlchemy ImportError: No module named flask_sqlalchemy
As far as im aware as im in my virtualenv and have my packages installed, this shouldnt happen. Trying to pip3 install on any packages shows them as already existing.
To further assist with the issue, i tried to run the script inside pycharm and i get the following error prints in the log:`Traceback (most recent call last):
File "/Users/Jack/Developer/Python/lift_api/run.py", line 26, in <module>
sqlAlchemy.create_all()
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 1007, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 999, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 941, in get_engine
return connector.get_engine()
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 543, in get_engine
self._engine = rv = sqlalchemy.create_engine(info, **options)
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/engine/__init__.py", line 387, in create_engine
return strategy.create(*args, **kwargs)
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 80, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 554, in dbapi
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'`
Anyone have an idea?
To run your code with python 3.x use python3 command.
python3 run.py
What is saying what it needs - psycopg2. If you are on Ubuntu it can help:
sudo apt install libpq-dev python-dev
Then don't forget to install flask-sqlalchemy and other libraries while your virtualenv activated.

Initializing H2O in Python

When trying to initialize H2O in Python by running the lines:
import h2o
h2o.init()
I getting the exceptions:
Traceback (most recent call last):
File "C:/workspace/DataScience/univariate_analysis/prudential_RF.py", line 4, in
h2o.init()
File "C:\Anaconda\lib\site-packages\h2o\h2o.py", line 668, in init
H2OConnection(ip=ip, port=port,start_h2o=start_h2o,enable_assertions=enable_assertions,license=license,max_mem_size_GB=max_mem_size_GB,min_mem_size_GB=min_mem_size_GB,ice_root=ice_root,strict_version_check=strict_version_check)
File "C:\Anaconda\lib\site-packages\h2o\connection.py", line 101, in __init__
raise EnvironmentError, message
EnvironmentError: Version mismatch. H2O is version 3.2.0.9, but the python package is version 3.6.0.8.
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Anaconda\lib\atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "C:\Anaconda\lib\site-packages\h2o\connection.py", line 565, in end_session
H2OConnection.delete(url_suffix="InitID")
File "C:\Anaconda\lib\site-packages\h2o\connection.py", line 393, in delete
return __H2OCONN__._do_raw_rest(url_suffix, "DELETE", None, **kwargs)
File "C:\Anaconda\lib\site-packages\h2o\connection.py", line 483, in _do_raw_rest
.format(http_result.status_code,http_result.reason,method,url,detailed_error_msgs))
EnvironmentError: h2o-py got an unexpected HTTP status code:
404 Not Found (method = DELETE; url = http://localhost:54321/3/InitID).
detailed error messages: DELETE /3/InitID not found
Error in sys.exitfunc:
Traceback (most recent call last):
File "C:\Anaconda\lib\atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "C:\Anaconda\lib\site-packages\h2o\connection.py", line 565, in end_session
H2OConnection.delete(url_suffix="InitID")
File "C:\Anaconda\lib\site-packages\h2o\connection.py", line 393, in delete
return __H2OCONN__._do_raw_rest(url_suffix, "DELETE", None, **kwargs)
File "C:\Anaconda\lib\site-packages\h2o\connection.py", line 483, in _do_raw_rest
.format(http_result.status_code,http_result.reason,method,url,detailed_error_msgs))
EnvironmentError: h2o-py got an unexpected HTTP status code:
404 Not Found (method = DELETE; url = http://localhost:54321/3/InitID).
detailed error messages: DELETE /3/InitID not found
Please note I have followed the installation steps in this link Use H2O directly from Python
You have a version mismatch between your h2o python code and your h2o java code.
If you download a fresh version of h2o, make sure you also do a pip install with the same version number. Don't use a generic pip install... rather specify the url and version explicitly.
Download the java package and start the java jar:
But then also click the install python link to get the corresponding pip install command:
Notice that this line needs to match the java version number that you just installed:
pip install http://h2o-release.s3.amazonaws.com/h2o/reltibshirani/8/Python/h2o-3.6.0.8-py2.py3-none-any.whl
Then try again:
import h2o
h2o.init()
Hope this helps!
EnvironmentError: Version mismatch. H2O is version 3.2.0.9, but the python package is version 3.6.0.8.
It looks like you have an instance of 3.2.0.9 already running and the Python package, which is 3.6.0.8, is complaining due to incompatibility.
I recommend you kill the current running instance of H2O (the 3.2.0.9 version that you see in the error message) and run the same commands (import h2o, h2o.init) to drive H2O from Python cleanly.

configparser not working in Python 3.4, NoSectionError but works fine in PyCharm

I worked on a Python 3.4 script in PyCharm 4.5. (repo: https://github.com/Djidiouf/bbot )
In it, I used import configparser without any problem for retrieving some values in a config.cfg:
config = configparser.RawConfigParser()
config.read('config.cfg')
server = config.get('bot_configuration', 'server')
channel = config.get('bot_configuration', 'channel')
botnick = config.get('bot_configuration', 'botnick')
port = config.getint('bot_configuration', 'port')
Now, I want to deploy it on a Debian 8.1 server. On that server, I have both Python 2.7 and Python 3.4 versions installed.
But when I ran the script on the machine, I have the following issue:
djidiouf#linuxserver:/home/djidiouf# /usr/bin/python3.4 /home/djidiouf/bbot/bbot.py
Traceback (most recent call last):
File "/usr/lib/python3.4/configparser.py", line 1116, in _unify_values
sectiondict = self._sections[section]
KeyError: 'bot_configuration'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/djidiouf/bbot/bbot.py", line 25, in <module>
import modules.steam # Contains specific Steam-Valve related functions
File "/home/djidiouf/bbot/modules/steam.py", line 13, in <module>
import modules.connection
File "/home/djidiouf/bbot/modules/connection.py", line 37, in <module>
server = config.get('bot_configuration', 'server')
File "/usr/lib/python3.4/configparser.py", line 754, in get
d = self._unify_values(section, vars)
File "/usr/lib/python3.4/configparser.py", line 1119, in _unify_values
raise NoSectionError(section)
configparser.NoSectionError: No section: 'bot_configuration'
I made a lot of research on Internet about that but I didn't find anything valuable. Some people made mention about a corrupt config.cfg file but mine is a LF EOL file with UTF8 without BOM as needed. I also recreated it again with vi on the machine.
I also tried to install configparser with pip (even if, if I understand it well, it's now build-in in Python 3) : pip3 install configparser But when I do that, I have the following error:
djidiouf#linuxserver:/home/djidiouf# sudo pip3 install -v configparser
[sudo] password for djidiouf:
Downloading/unpacking configparser
Ignoring link https://pypi.python.org/packages/source/c/configparser/configparser-3.5.0b1.tar.gz#md5=d60ca2c714acb4adaf5818c6a1ffd78b (from https://pypi.python.org/simple/configparser/), version 3.5.0b1 is a pre-release (use --pre to allow).
Ignoring link https://pypi.python.org/packages/source/c/configparser/configparser-3.5.0b2.tar.gz#md5=ad2a71db8bd9a017ed4735eac7acfa07 (from https://pypi.python.org/simple/configparser/), version 3.5.0b2 is a pre-release (use --pre to allow).
Using version 3.3.0r2 (newest of versions: 3.3.0r2, 3.3.0r1, 3.2.0r3, 3.2.0r2, 3.2.0r1)
Downloading configparser-3.3.0r2.tar.gz
Downloading from URL https://pypi.python.org/packages/source/c/configparser/configparser-3.3.0r2.tar.gz#md5=dda0e6a43e9d8767b36d10f1e6770f09 (from https://pypi.python.org/simple/configparser/)
Running setup.py (path:/tmp/pip-build-v7av3db7/configparser/setup.py) egg_info for package configparser
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 5, in <module>
import distutils.core
File "/usr/lib/python3.4/distutils/core.py", line 18, in <module>
from distutils.config import PyPIRCCommand
File "/usr/lib/python3.4/distutils/config.py", line 7, in <module>
from configparser import ConfigParser
File "/tmp/pip-build-v7av3db7/configparser/configparser.py", line 397
_KEYCRE = re.compile(ur"%\(([^)]+)\)s")
^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 5, in <module>
import distutils.core
File "/usr/lib/python3.4/distutils/core.py", line 18, in <module>
from distutils.config import PyPIRCCommand
File "/usr/lib/python3.4/distutils/config.py", line 7, in <module>
from configparser import ConfigParser
File "/tmp/pip-build-v7av3db7/configparser/configparser.py", line 397
_KEYCRE = re.compile(ur"%\(([^)]+)\)s")
^
SyntaxError: invalid syntax
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-v7av3db7/configparser
Exception information:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 290, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/usr/lib/python3/dist-packages/pip/req.py", line 1230, in prepare_files
req_to_install.run_egg_info()
File "/usr/lib/python3/dist-packages/pip/req.py", line 326, in run_egg_info
command_desc='python setup.py egg_info')
File "/usr/lib/python3/dist-packages/pip/util.py", line 716, in call_subprocess
% (command_desc, proc.returncode, cwd))
pip.exceptions.InstallationError: Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-v7av3db7/configparser
Storing debug log for failure in /root/.pip/pip.log
Here is a config example, I just changed the values and rename the file accordingly to config.cfg :
[bot_configuration]
server = your.irc.server.com
channel = ##your_channel
botnick = bbot
port = 6667
admins = You,Him
https://github.com/Djidiouf/bbot/blob/master/config_example.cfg
Finally, I just want to say that everything is working on PyCharm 4.5, I just made a git clone, rename the config_example.cfg, change the values, tried to run the script with Python 3.4.
The install error is from _KEYCRE = re.compile(ur"%\(([^)]+)\)s"), that is causing a syntax error as the ur prefix is not supported in python3, pip is trying to instal a python2 package.
Your first error is a keyError 'bot_configuration' which is not because of not having configparser installed, configparser is part of the standard library.
The bot_configuration KeyError is because python cannot find your config file, if you use config.read('foobar') in pycharm you will see the exact same output
I figured out that I had some inconsistency about running the script. When I was in another folder than my script, nothing worked but when I was in it, it worked. Perhaps because the config file is shared and used in the main script and in a module.
In fact it's due to the need to have an absolute path in config.read('/some/path/file.cfg')
Finally, I find that answer: https://stackoverflow.com/a/13800583/3301370
So here is the solution in the case of config.cfg being on the project root directory /config.cfg :
/bbot.py
import os
config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'config.cfg'))
However, as the config file is shared and used in the main script and in a subpath, module, a correct path must be set for the sub module too:
/modules/connection.py
import os
config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'config.cfg'))
It works fine in PyCharm because when you hit the run button for your script, it runs it with the path of the project. The fact that having a relative path for both config.read() statements worked only because of that.
The problem with installing configparser with pip is unrelated to that specific problem and can be ignore in that case as configparser is a built-in library in Python3.

DistributionNotFound when trying to run console script in setup.py

I created a virtualenv with python2.7 a few weeks ago and got this project running fine. It uses setuptools and has a few console scripts defined.
Now today I installed python3 and pip3, which I didn't think would conflict with this project but I'm running into this error when I try to run the same console script.
pkg_resources.DistributionNotFound: myproject==1.0.0.0-local
Traceback (most recent call last):
File "/Users/me/dev/myproject/venv-myproject/bin/run_queue_worker_manager", line 5, in <module>
from pkg_resources import load_entry_point
File "/Users/me/dev/project/venv-myproject/lib/python2.7/site-packages/pkg_resources.py", line 2720, in <module>
parse_requirements(__requires__), Environment()
File "/Users/me/dev/project/venv-myproject/lib/python2.7/site-packages/pkg_resources.py", line 588, in resolve
raise DistributionNotFound(req)

Categories