Python Forum
Running pytest gives " 'cannot import name 'session' " error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running pytest gives " 'cannot import name 'session' " error
#1
I am writing a Python script, which returns topics from a Discourse forum based on a user-entered search query.

CONTEXT

To start, I've built an API wrapper for the Discourse API. The code is available here https://github.com/jasonblais/mattermost-helpdesk

The structure is simple:

.
├── requirements.txt
├── tests
│ ├── test_discourse_wrapper.py // tests my wrapper is working as expected
└── discourse_api_wrapper
│ ├── __init__.py // initializer that creates a requests session used for all HTTP interactions with the Discourse API
│ ├── discourse.py // creates a Discourse class, which makes an API call to Discourse and returns the response

ISSUE

To run the tests I execute this command from the root directory. Note that DISCOURSE_API_KEY is a parameter in discourse_api_wrapper/__init__.py

DISCOURSE_API_KEY='<my-api-key>' python3 -m pytest tests/

I get the following output

Output:
============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.0.3, py-1.6.0, pluggy-0.4.0 rootdir: /.../mattermost-helpdesk, inifile: collected 0 items / 1 errors ==================================== ERRORS ==================================== _______________ ERROR collecting tests/test_discourse_wrapper.py _______________ ImportError while importing test module '/.../mattermost-helpdesk/tests/test_discourse_wrapper.py'. Original error message: 'cannot import name 'session' from 'discourse_api_wrapper' (.../mattermost-helpdesk/discourse_api_wrapper/__init__.py)' Make sure your test modules/packages have valid Python names. !!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!! =========================== 1 error in 0.36 seconds ============================
How do I resolve this error, or troubleshoot it?

I'm new to Python (and programming in general) and doing a Google search didn't resolve it thus far.

Many thanks in advance!
Reply
#2
I don't know how you're getting that error, since you're not trying to import the session. Is the code on github the same code that generates the error?
Reply
#3
Thanks for the reply!

It is the same code, I just pushed it upstream this morning to GitHub before creating this thread.

Could I be missing a package I have to first install for Python?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "cannot identify image file" part way through running hatflyer 0 615 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 2,892 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,169 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Error when running kivy on python janeik 8 1,926 Jun-16-2023, 10:58 PM
Last Post: janeik
  Pytest mocks anthonyrmoss78 0 427 May-30-2023, 08:28 PM
Last Post: anthonyrmoss78
  Why is this import running twice? extan1 10 2,713 Feb-17-2023, 02:02 AM
Last Post: extan1
  import module error tantony 5 3,362 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Getting error when running "MINUS" between 2 databases marlonbown 4 1,217 Nov-10-2022, 05:49 AM
Last Post: deanhystad
  Error while running code on VSC maiya 4 3,550 Jul-01-2022, 02:51 PM
Last Post: maiya
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,321 Jun-18-2022, 01:09 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020