question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PicklingError using live_server fixture on Windows

See original GitHub issue

Not sure if Windows is supposed to be supported, but for me it fails on Windows 10 Python 2.7.12 with the following error when live_server fixture used:

request = <SubRequest '_push_request_context' for <Function 'test_get_url'>>                                                                                       
                                                                                                                                                                   
    @pytest.fixture(autouse=True)                                                                                                                                  
    def _push_request_context(request):                                                                                                                            
        """During tests execution request context has been pushed, e.g. `url_for`,                                                                                 
        `session`, etc. can be used in tests as is::                                                                                                               
                                                                                                                                                                   
            def test_app(app, client):                                                                                                                             
                assert client.get(url_for('myview')).status_code == 200                                                                                            
                                                                                                                                                                   
        """                                                                                                                                                        
        if 'app' not in request.fixturenames:                                                                                                                      
            return                                                                                                                                                 
                                                                                                                                                                   
        app = request.getfuncargvalue('app')                                                                                                                       
                                                                                                                                                                   
        # Get application bound to the live server if ``live_server`` fixture                                                                                      
        # is applyed. Live server application has an explicit ``SERVER_NAME``,                                                                                     
        # so ``url_for`` function generates a complete URL for endpoint which                                                                                      
        # includes application port as well.                                                                                                                       
        if 'live_server' in request.fixturenames:                                                                                                                  
>           app = request.getfuncargvalue('live_server').app                                                                                                       
                                                                                                                                                                   
..\..\..\..\python-virtualenv\shadow\Lib\site-packages\pytest_flask\plugin.py:85:                                                                                  
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _                                                                                    
..\..\..\..\python-virtualenv\shadow\Lib\site-packages\pytest_flask\fixtures.py:127: in live_server                                                                
    server.start()                                                                                                                                                 
..\..\..\..\python-virtualenv\shadow\Lib\site-packages\pytest_flask\fixtures.py:64: in start                                                                       
    self._process.start()                                                                                                                                          
c:\python27\Lib\multiprocessing\process.py:130: in start                                                                                                           
    self._popen = Popen(self)                                                                                                                                      
c:\python27\Lib\multiprocessing\forking.py:277: in __init__                                                                                                        
    dump(process_obj, to_child, HIGHEST_PROTOCOL)                                                                                                                  
c:\python27\Lib\multiprocessing\forking.py:199: in dump                                                                                                            
    ForkingPickler(file, protocol).dump(obj)                                                                                                                       
c:\python27\Lib\pickle.py:224: in dump                                                                                                                             
    self.save(obj)                                                                                                                                                 
c:\python27\Lib\pickle.py:331: in save                                                                                                                             
    self.save_reduce(obj=obj, *rv)                                                                                                                                 
c:\python27\Lib\pickle.py:425: in save_reduce                                                                                                                      
    save(state)                                                                                                                                                    
c:\python27\Lib\pickle.py:286: in save                                                                                                                             
    f(self, obj) # Call unbound method with explicit self                                                                                                          
c:\python27\Lib\pickle.py:655: in save_dict                                                                                                                        
    self._batch_setitems(obj.iteritems())                                                                                                                          
c:\python27\Lib\pickle.py:687: in _batch_setitems                                                                                                                  
    save(v)                                                                                                                                                        
c:\python27\Lib\pickle.py:286: in save                                                                                                                             
    f(self, obj) # Call unbound method with explicit self                                                                                                          
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _                                                                                    
                                                                                                                                                                   
self = <multiprocessing.forking.ForkingPickler instance at 0x0000000007EDBA08>                                                                                     
obj = <function <lambda> at 0x00000000081B9DD8>, name = '<lambda>'                                                                                                 
pack = <built-in function pack>                                                                                                                                    
                                                                                                                                                                   
    def save_global(self, obj, name=None, pack=struct.pack):                                                                                                       
        write = self.write                                                                                                                                         
        memo = self.memo                                                                                                                                           
                                                                                                                                                                   
        if name is None:                                                                                                                                           
            name = obj.__name__                                                                                                                                    
                                                                                                                                                                   
        module = getattr(obj, "__module__", None)                                                                                                                  
        if module is None:                                                                                                                                         
            module = whichmodule(obj, name)                                                                                                                        
                                                                                                                                                                   
        try:                                                                                                                                                       
            __import__(module)                                                                                                                                     
            mod = sys.modules[module]                                                                                                                              
            klass = getattr(mod, name)                                                                                                                             
        except (ImportError, KeyError, AttributeError):                                                                                                            
            raise PicklingError(                                                                                                                                   
                "Can't pickle %r: it's not found as %s.%s" %                                                                                                       
>               (obj, module, name))                                                                                                                               
E           PicklingError: Can't pickle <function <lambda> at 0x00000000081B9DD8>: it's not found as pytest_flask.fixtures.<lambda>                                

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:12

github_iconTop GitHub Comments

4reactions
ankostiscommented, Sep 27, 2019

Improving on @Jitsusama workaround, using flask run command to launch app to properly configure, and moved server-launch in ./tests/conftest.py:

import pytest
import socket
import subprocess

@pytest.fixture(scope="session")
def flask_port():
    ## Ask OS for a free port.
    #
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(("", 0))
        addr = s.getsockname()
        port = addr[1]
        return port

@pytest.fixture(scope="session", autouse=True)
def live_server(flask_port):
    env = os.environ.copy()
    env["FLASK_APP"] = "your_package.app_module"
    server = subprocess.Popen(['flask', 'run', '--port', str(flask_port)], env=env)
    try:
        yield server
    finally:
        server.terminate()

And then write your TestCases in test-files (e.g. ./tests/test_my_app.py) like this:

import pytest
import requests

def test_request(flask_port):
    response = requests.get(f"http://localhost:{flask_port}"')
    assert response.status_code == 200
2reactions
Nagasaki45commented, Jul 13, 2017

I don’t know if another report of the issue will help in anyway, but here it is:

============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
rootdir: C:\Users\tgurion\unity\Unsocial VR\backchannel, inifile:
plugins: flask-0.10.0
collected 1 item

test_server.py E

=================================== ERRORS ====================================
_____________ ERROR at setup of test_server_not_completely_broken _____________

request = <SubRequest '_push_request_context' for <Function 'test_server_not_completely_broken'>>

    @pytest.fixture(autouse=True)
    def _push_request_context(request):
        """During tests execution request context has been pushed, e.g. `url_for`,
        `session`, etc. can be used in tests as is::
    
            def test_app(app, client):
                assert client.get(url_for('myview')).status_code == 200
    
        """
        if 'app' not in request.fixturenames:
            return
    
        app = request.getfuncargvalue('app')
    
        # Get application bound to the live server if ``live_server`` fixture
        # is applyed. Live server application has an explicit ``SERVER_NAME``,
        # so ``url_for`` function generates a complete URL for endpoint which
        # includes application port as well.
        if 'live_server' in request.fixturenames:
>           app = request.getfuncargvalue('live_server').app

..\..\..\AppData\Local\Continuum\Anaconda3\envs\backchannel\lib\site-packages\pytest_flask\plugin.py:85: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\AppData\Local\Continuum\Anaconda3\envs\backchannel\lib\site-packages\pytest_flask\fixtures.py:127: in live_server
    server.start()
..\..\..\AppData\Local\Continuum\Anaconda3\envs\backchannel\lib\site-packages\pytest_flask\fixtures.py:64: in start
    self._process.start()
..\..\..\AppData\Local\Continuum\Anaconda3\envs\backchannel\lib\multiprocessing\process.py:105: in start
    self._popen = self._Popen(self)
..\..\..\AppData\Local\Continuum\Anaconda3\envs\backchannel\lib\multiprocessing\context.py:223: in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
..\..\..\AppData\Local\Continuum\Anaconda3\envs\backchannel\lib\multiprocessing\context.py:322: in _Popen
    return Popen(process_obj)
..\..\..\AppData\Local\Continuum\Anaconda3\envs\backchannel\lib\multiprocessing\popen_spawn_win32.py:65: in __init__
    reduction.dump(process_obj, to_child)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

obj = <Process(Process-1, initial)>, file = <_io.BufferedWriter name=8>
protocol = None

    def dump(obj, file, protocol=None):
        '''Replacement for pickle.dump() using ForkingPickler.'''
>       ForkingPickler(file, protocol).dump(obj)
E       AttributeError: Can't pickle local object 'LiveServer.start.<locals>.<lambda>'

..\..\..\AppData\Local\Continuum\Anaconda3\envs\backchannel\lib\multiprocessing\reduction.py:60: AttributeError
=========================== 1 error in 0.89 seconds ===========================
Read more comments on GitHub >

github_iconTop Results From Across the Web

Having an issue connecting my LiveServer to Google Chrome
My live-server used to go in Microsoft Edge, but I have to change its location for class, and I have had some trouble...
Read more >
Why Visual Studio Code Live Server Not Working? - YouTube
Live Server extension works fine with most of the machines but some of you might face an issue, it's not working perfectly on...
Read more >
Live Server - Visual Studio Marketplace
Open a project and click to Go Live from the status bar to turn the server on/off. · Right click on a HTML...
Read more >
Live.server will not work in Visual code - Microsoft Community
Hello, My setup is as follows: Version: 1.44.2 (user setup) Commit: ff915844119ce9485abfe8aa9076ec76b5300ddd Date: 2020-04-16T16:36:23.138Z ...
Read more >
Settings | Live Server | VSCode Extension - Ritwick Dey
microsoft -edge. Not enough? need more? open an/a issue/pull request on github. For now, use liveServer.settings.AdvanceCustomBrowserCmdLine ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found