Can't debug unittests in Pycharm - python

I'm having trouble debugging unit tests in pycharm. I can run them fine with my configuration but when I run the debugger I get the following error output:
Error
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: tests
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/Users/paymahn/solvvy/scheduler/tests/__init__.py", line 2, in
import tests.test_setup_script
File "/Users/paymahn/solvvy/scheduler/tests/test_setup_script.py", line 3, in
import setup
File "/Applications/PyCharm.app/Contents/helpers/pydev/setup.py", line 87, in
data_files.append(('pydevd_attach_to_process', [os.path.join('pydevd_attach_to_process', f) for f in os.listdir('pydevd_attach_to_process') if accept_file(f)]))
FileNotFoundError: [Errno 2] No such file or directory: 'pydevd_attach_to_process'
My directory structure is:
My unittest configuration is:
tests/test_setup_script.py looks like:
import unittest
import os
import setup # the path to this file is scheduler/setup.py. This import may be breaking things
class TestSetupScript(unittest.TestCase):
def test_load_db_connection_valid_yaml_file(self):
file_name = "valid-yaml.yml"
with open(file_name, 'w') as file:
file.write("""
test:
hello world
a = b
""")
yaml = setup.load_yaml_configuration(file_name)
# I want to debug the line above, hence no assertions here
What does pydevd_attach_to_process do and how can I make sure it's found during debugging? Is the problem not actually related to that file/directory being found?

Indeed, the failure occurs because the "import setup" in your code imports the setup module which is part of PyCharm's debugger runtime instead of your own setup module. The easiest fix to this is to rename your setup.py file to something else and to update the imports in your code accordingly.

Related

NameError: name 'logging' is not defined

I am getting an error when trying to run a python script in Linux. I am trying to run a Firewall configuration converter I have dowloaded from here:
https://github.com/glennake/DirectFire_Converter
(converter.py file)
First I was getting an error related to logger, but after running a pip3 install logger it has been fixed. Now I am getting NameError: name 'logging' is not defined, despite of code looking good as far I know. For some reason, it looks like this line is not working:
logger = logging.getLogger(__name__)
But I am seeing that all modules are being imported properly. Any idea about what is bringing the issue? Thanks.
full error traceback:
Traceback (most recent call last):
File "converter1.py", line 257, in <module>
main(src_format=args.source, dst_format=args.destination, routing_info=args.routing)
File "/home/ubuntu/.local/lib/python3.8/site-packages/traceback_with_variables/print.py", line 98, in wrapper
return func(*args, **kwargs)
File "converter1.py", line 233, in main
parsed_data = parse(
File "converter1.py", line 107, in parse
from DirectFire.Converter.parsers.ciscoasa_pre83 import parse
File "/home/ubuntu/DirectFire_Converter/DirectFire/Converter/parsers/ciscoasa_pre83.py", line 23, in <module>
logger = logging.getLogger(__name__)
NameError: name 'logging' is not defined
You need to import, add import logging at the top of your file

Pycall throws ValidationError

I'm trying to run these lines of code in atom and python3.6 :
from pycall import CallFile, Call, Application
import sys
def call():
c = Call('SIP/200')
a = Application('Playback', 'hello-world')
cf = CallFile(c, a)
cf.spool()
if __name__ == '__main__':
call()
But I receive this error:
Traceback (most recent call last):
File "/home/pd/gits/voiphone/main.py", line 12, in <module>
call()
File "/home/pd/gits/voiphone/main.py", line 9, in call
cf.spool()
File "/home/pd/telephonerelayEnv/lib/python3.6/site-packages/pycall/callfile.py", line 135, in spool
self.writefile()
File "/home/pd/telephonerelayEnv/lib/python3.6/site-packages/pycall/callfile.py", line 123, in writefile
f.write(self.contents)
File "/home/pd/telephonerelayEnv/lib/python3.6/site-packages/pycall/callfile.py", line 118, in contents
return '\n'.join(self.buildfile())
File "/home/pd/telephonerelayEnv/lib/python3.6/site-packages/pycall/callfile.py", line 100, in buildfile
raise ValidationError
pycall.errors.ValidationError
I would appreciate if you help me solving my problem.
thank you in advance
Looking at the source code for the validity check, it appears like the only check that could be catching you out is the one that verifies the spool directory. By default this is set to /var/spool/asterisk/outgoing but can be changed when you create the callfile:
cf = CallFile(c, a, spool_dir='/my/asterisk/spool/outgoing')

Package level logging in python

I am trying to set up package level logging via my init.py file.
I have been testing this in virtual environment but cannot seem to understand the error.
My package structure is as so:
extracts
extracts
__init__.py
logconfig.ini
methods.py
foo.py
setup.py
requirements.txt
setup.py looks like so:
setup(name='extracts',
version='0.0.1',
description='Extracts',
packages=['extracts'],
package_data={'extracts': ['*.ini']},
install_requires=requirements,
zip_safe=False
)
init.py is causing me problems.
logfile = resource_stream(__name__, 'logconfig.ini')
logging.config.fileConfig(logfile)
log = logging.getLogger(__name__)
log.info('Importing extracts 0.0.1')
foo.py is the script i'm trying to run. It import functions from module 'methods.py'. When that occurs, init.py should be triggered and set up logging for foo.py. Unfortunately everytime I try this, I get the following error:
Traceback (most recent call last):
File "test.py", line 2, in <module>
from extracts import methods
File "build/bdist.linux-i686/egg/extracts/__init__.py", line 19, in <module>
logfile = resource_stream(__name__, 'logconfig.ini')
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 942, in resource_stream
self, resource_name
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1346, in get_resource_stream
return BytesIO(self.get_resource_string(manager, resource_name))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1349, in get_resource_string
return self._get(self._fn(self.module_path, resource_name))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1425, in _get
return self.loader.get_data(path)
IOError: [Errno 0] Error: 'extracts/logconfig.ini'
https://pythonhosted.org/setuptools/pkg_resources.html#basic-resource-access mentions that the package_or_requirement in resource_stream(package_or_requirement, resource_name) needs to be importable. But what I don't understand is that the extracts pacakge is importable since
from extracts import methods
does not fail. Any ideas are greatly appreciated. Thank you.

Python makes lib error when making FireFox Sync 1.5

I've tried to make my own server to new sync service in FireFox.
I used the following tutorial: https://docs.services.mozilla.com/howtos/run-sync-1.5.html .
When I wanted to check it by using make test I got the following error:
Traceback (most recent call last):
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/nose/loader.py", line 403, in loadTestsFromName
module = resolve_name(addr.module)
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/nose/util.py", line 311, in resolve_name
module = __import__('.'.join(parts_copy))
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/syncstorage/__init__.py", line 5, in <module>
import mozsvc.config
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/mozsvc/config.py", line 10, in <module>
from konfig import Config, SettingsDict
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/konfig/__init__.py", line 11, in <module>
from configparser import ConfigParser, ExtendedInterpolation
ImportError: cannot import name ExtendedInterpolation
What's happened?
configparser.ExtendedInterpolation does not exist in Python 2.
You need to use Python 3.

Impossible to create a configuration file for an python application inside Google App Engine

I try to create a configuration file, where I can store constants.
Whenever I try with ConfigParser, I get an error
Traceback (most recent call last):
File "/home/baun/google_appengine/google/appengine/ext/webapp /__init__.py", line 511, in __call__
handler.get(*groups)
File "/home/baun/workspace/octopuscloud/s3/S3.py", line 138, in get
test = parser.get('bucket', 'bucketname')
File "/usr/lib/python2.5/ConfigParser.py", line 511, in get
raise NoSectionError(section)
NoSectionError: No section: 'bucket'
simple.cfg:
[bucket]
bucketname: 'octopus_storage'
s3.py:
...
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('simple.cfg')
...
# Get values from the config file
test = parser.get('bucket', 'bucketname')
...
How can I fix this?
===============================
The problem is fixed. The code was correct, but simple.cfg was in the wrong directory.
[bucket]
bucketname= octopus_storage

Categories