Import issue with Pycharm

Question:

I am writing a module in Python called silk.jira.create.create

In that module I’m trying to import the jira third party module that I installed with pip.

from jira import JIRA

When I run the module I see a Error:

ImportError: cannot import name 'JIRA' from 'jira'

looking in the debug console I can see PyCharm is confused and has imported the wrong module. ie


jira.__file__
'C:\<full path>\silk\jira\__init__.py' <- I'm expecting the third party here

If I run this outside of PyCharm with python -m it works (no ImportError).

Any ideas what’s going on here?

Asked By: pcauthorn

||

Answers:

I can see that you are using windows.

Pycharm doesn’t have python inside, it uses the python installed in your system. You can have multiple installation of python in the same system.

Firstly, check where you have installed python and if you have multiple installations.

You could have it installed using the microsoft store, from the python3 website or from a packages manager like anaconda or miniconda.

Then select the correct interpreter using this pycharm guide after you figure it out.

https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html

Answered By: anarchy

PyCharm includes your working directories, including your module folder if you labeled it as source dir, into PYTHONPATH. By doing so you got two modules named jira in your environment.

You should either rename your module (highly advisable in general to avoid confusion and interference!) or remove the specific path from the python interpreter that you set on PyCharm. To do so, go to Python Interpreter (bottom right on status bar), then Show All and click on the directory trees.

By doing so, you might not be able to see your module at all when importing. You have to use import declaration relative to your project root directory

Answered By: Buzz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.