"No module named xx" when editing file, but python executes it fine

Answered

(Apologies in advance that I am pretty new to Python and Pycharm..)

We have a Python library package stored in our git repository.  This has a setup.py file using setuptools and creates an egg when run.

In another application, we have another setup.py file which lists the library in the install_requires section.  It also lists it in dependency_links.  So it looks a bit like:

install_requires= [ 'my_library == 1.0.0' ],

dependency_links = [ 

'git+ssh://git@our.git.repo:22/path-to-git@branch#egg=my_library-1.0.0

]

After installing (python setup.py install from terminal within the virtual env.),, I can see in my venv site-packages that there is a "my_library-1.0.0-py3.5.egg.  Opening this up I can see the files pulled down from git and all seems fine.

When I run my application, imports such as "import my_module" that come from this dependency work fine.  However, PyCharm put red wiggly underlines and says "No module named my_module".

(By "run" I mean from the terminal or right clicking and running from Pycharm)

I'm using a virtualenv (which I created through pycharm), Python version 3.5.2 on Ubuntu.  I can see this set up as the interpreter in project settings.

Any idea how I can make this work?  

3
15 comments
Avatar
Permanently deleted user

Can you successfully run your code that has "import my_module" inside PyCharm using the "Run" action from the context menu of a file with this import?

Please check that your sys.path and your File | Settings | Project | Project Interpreter | More | Show Paths are the same.

2

Slightly embarrassing but I quit PyCharm and restarted and now it seems OK.  I wonder if PyCharm (and then me!) got confused because I'd been working on a related problem which I resolved, but then PyCharm stayed "broken".  Although I had tried turning it off and on again, I obviously didn't once I got the other problem resolved!

Whilst it was broken, right clicking and running the file worked fine.

I never knew you could list the paths like that from Pycharm.  If it happens again that's the first place I will look.

Thanks very much for the quick and helpful reply - apologies that it turned out to be a silly thing, but you've still given me something useful all the same.

2

I have a very similar problem, and haven't been able to solve it after trying a number of ways.

Situation: I am using Windows 10, and both Python 2.7 and Python 3.6 are installed; the folders for the former is before the latter in the PATH variable. The project is configured to use Python 3.6 (using the system installation or a local virtual environment has the same outcome). PyCharm says: "No module named math". Needless to say, the module is available and the script works. 

What hasn't worked: As hinted above, I deleted the interpreter settings, then re-configured it, either to the system installation or to use a virtual environment. I deleted the cache and restarted it. I checked if PYTHON_PATH is set (it is not). 

This problem seems to be prevalent among PyCharm users. PyCharm is a great environment, but it would be sooo good if you could solve this issue as well.

Thanks!

2

The same problem after last pyCharm update (2017.3).

It seems like no math module installed, but if I run the same file (right click, run) - there's no problem.

 
0
Avatar
Permanently deleted user

Please update to PyCharm 2017.3.1.

0

I updated to PyCharm 2017.3.1 today and I'm seeing this same issue with `math` module. Doing "Invalidate caches and restart" doesn't help. Nor does deleting the interpreter definition and re-adding it.  The interpreter I'm currently using is Python 3.6.2 for Windows 64 bit.

0

I am using PyCharm 2017.3.1 as well.  I am on Mac OS X El Capitan (10.11.6), running Python 2.7.10 with a virtualenv interpreter.  Using a long-existing PyCharm project, the following standard modules trigger the "No module named {module}" IDE error:  time, operator, math

I created a brand new virtualenv along with a new PyCharm project, and made it available to all projects.  The above mentioned errors went away.  I then deleted both the project and the virtualenv, finally restarting PyCharm.  The errors did not reappear.

Seems like a weird way to fix this issue, but this procedure worked for me.

0

None of these workarounds are working for me. I've tried several new Java projects (adding the Python interpreter) different virtualenvs and even python on outside a virtualenv and nothing works. I'm running on a mac with High Sierra with IntelliJ 2017.3.2.

0

I had a similar issue with IntelliJ IDEA Ultimate 2018.2. My simple Python script starts with import sys and IDEA was flagging sys with "No module named sys". After invalidating the caches and restarting, the issue went away. Of course, YMMV.

0

I faced this problem recently. Some old settings left after i reinstalled Windows.

I used following steps to solve the problem:

1) recreate project 

2) reconfigure python plugin

0

I'm facing the same issue with IntelliJ Ultimate Edition.

0

>I'm facing the same issue with IntelliJ Ultimate Edition.

Have you tried "File >> Invatidate caches / restart" and recreating python interpreter?

1

Thanks for looking into it. I think I face the issue as soon as I add __init__.py file to any package. Once I delete the file, the errors go away.

0

Hi, I had a similar problem where I was importing another script as a module for the first time. I have two .py files in a directory called "importing_local_scripts": my_script.py and useful_functions.py. I tried to import useful_functions.py into my_script.py using:

import useful_functions as uf

and it worked when I ran my_script.py in Git Bash (I could access the functions in the other script) but there was a red squiggly line under "useful_functions" in PyCharm and it said "no module named useful_functions" when I moused over it.

I'm new to Python and programming so I'm not sure if this has any repercussions since I don't know what it means exactly, but I right clicked the directory "importing_local_scripts" (where my .py files are) in the PyCharm project explorer and went to Mark Directory as -> Sources Root, which turns the directory blue and fixes the problem.

Not sure if this fixes the original poster's problem, but this thread came up when I searched for my problem and this fixed it for me so hopefully it helps someone else :)!

5

Thank you, +1 for the suggestion to mark the source directory in the project settings. Our scripts ran fine because the path was imported but the IDE did not detect it (I'm copying this code here for search engines)

sys.path.insert(0,'../isd_handler')
## this import had the red line in IntelliJ, but the script found it and ran fine
import imd_handler

Here is a picture of the setting that was changed for the IDE setting to find the module:

 

 

0

Please sign in to leave a comment.