1

I've created a project in Pycharm that I can successfully run and as well its unit tests. However, when I try to run pytest from the command line in the location where the tests folder is I get an error where the tests protest that it can't find objects in the given module (under test). I know that Pycharm runs certain commands before launching anything and exports the information through PYTHONPATH to make its tests work but how do I do the same thing without PyCharm?

The following shows my project structure:

PycharmProjects\
     ProjectFolder\
         - setup.py
         - readme.txt
         module\
             - __init__.py
             - module.py
         tests\
             - __init__.py
             - test_module.py

Before Pycharm runs my code, it does the following at the time when the script starts up (I got this from Settings > Console > Python Console). I don't know how to tell what is inside WORKING_DIR_AND_PYTHON_PATHS.

import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])

However, when I run from the command line when I am at the Project Folder I get the following type of error. What gives??

D:\user\development\PycharmProjects\ProjectFolder> pytest
============================= test session starts =============================
platform win32 -- Python 2.7.11, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
rootdir: D:\user\PycharmProjects\ProjectFolder, inifile:
collected 4 items

tests\test_module.py .F.F

================================== FAILURES ===================================
_______________________ TestModule.test_ONE ________________________

self = <tests.test_module.TestModule testMethod=test_ONE>

    def test_ONE(self):
>       someObject = module.SomeObject("someObject.log")
E       AttributeError: 'module' object has no attribute 'SomeObject'

tests\test_module.py:23: AttributeError
________________________ TestModule.test_TWO _________________________

self = <tests.test_module.TestModule testMethod=test_TWO>

    def test_TWO(self):
>       someObject = module.SomeObject("someObject.log")
E       AttributeError: 'module' object has no attribute 'SomeObject'

tests\test_module.py:8: AttributeError
===================== 2 failed, 2 passed in 0.14 seconds ======================
4
  • Is your PYTHONPATH=D:\user\PycharmProjects\ProjectFolder? What does your import statement inside of tests\test_module.py look like for module? In this scenario it would need to be from module import module, and SomeObject would be a class inside D:\user\PycharmProjects\ProjectFolder\module\module.py Commented Jul 11, 2017 at 2:18
  • 1). PYTHONPATH=D:\user\PycharmProjects\ProjectFolder 2). Inside tests\test_module.py, the import says import module; 3). I tried the from module import module and I get the following error now in PyCharm (but the command line now works) --- from module import module, ImportError: cannot import name module Commented Jul 11, 2017 at 3:01
  • Why do the two (pycharm and command line) fight with each other in terms of settings?? Commented Jul 11, 2017 at 3:06
  • I'm guessing your project structure isn't set up properly in PyCharm to be compatible with how you are trying to do things on the command line. If you made PYTHONPATH=D:\user\PycharmProjects\ProjectFolder;D:\user\PycharmProjects\ProjectFolder\module and your import statement import module I bet it would work in both, but that's not really the correct way of doing it. Try changing your project content root Settings | Project | Project Structure to D:\user\PycharmProjects\ProjectFolder. If that doesn't work I'd probably need to see an image of your prj struct in PyCharm Commented Jul 11, 2017 at 14:48

1 Answer 1

1

You will want to set the PYTHONPATH environment variable to the directory that has your source package/module. So if your package is at C:\my_stuff\foo\my_package you will want your PYTHONPATH to be C:\my_stuff\foo. You will also want to make sure you have __init__.py files in all of the directories which contain source code.

You may also need to add your python area to the PATH env variable. If your python.exe file is at C:\Python27\python.exe, you would want C:\Python27 to be in PATH before any other python directories.

To set the env variable...

  1. Click Start button

  2. Click File Explorer

  3. Right click "This PC" -> Properties

  4. Click Advanced system settings

  5. Click Environment Variables...

  6. Under "User Variables for nndhawan" look for the variable PYTHONPATH. If it exists, click Edit.... If it doesn't exist, Click New...

  7. Variable Name: PYTHONPATH

  8. Variable Value: C:\my_stuff\foo Note that if you have multiple paths, they are separated with a semicolon and no spaces. Once you have multiple saved, the interface will look slightly different, but still pretty intuitive. Use Edit and New how you'd expect.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I've tried this and this didn't solve my problem. I'll update my ticket a little better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.