0

I am working on an exercise where I need to import a python module called ex25. However, I am getting the following error:

    Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import ex25
ImportError: No module named ex25

I already added all possible paths to my system path variables, so I'm not sure why it won't let me import.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3141348
  • 21
  • 1
  • 1
  • 1
  • other similar questions have been resolved by adding path of the folder where ex25.py located to the sys variables path, but i already did it – user3141348 Dec 31 '13 at 21:10
  • Without knowing what you think "all possible paths" includes, it's hard to be specific, so: What is the path to `ex25.py`, and what does `print sys.path` show? – abarnert Dec 31 '13 at 21:24
  • Also, are you sure you saved it as `ex25.py` rather than, say, `ex25` or `ext25.py.txt` (which can happen on Windows when Notepad and Explorer are conspiring to hide file extensions from you)? – abarnert Dec 31 '13 at 21:25
  • user3141348 I added that path to sys var path and I enabled displaying of file extensions. It works now, but i want to explain better, in case someone has same problem. My default cmd path is C:Users\UserExample(it's not added to system path env var), it lets me run any .py script without issues but wont let me import ex25. Once i changed path in cmd to the one where file ex25.py located it imports just fine. – user3141348 Dec 31 '13 at 21:49
  • 3
    This question appears to be off-topic because the basic premise is not true. The problem is exactly what the question emphatically says isn't the problem. – abarnert Dec 31 '13 at 22:08

1 Answers1

1

The only problem is that, despite what your question and your first comment say, you didn't add the path containing ex25.py to your system paths.

As evidenced by the fact that, once you actually did what you claimed, everything worked fine.

For anyone with a similar problem in the future, just add this above the import:

import sys
print(sys.path)

(If the parentheses look weird, they're just to make the same code work for both Python 2.x and 3.x.)

Now, when you run it, you'll see a list of paths. Is the path to ex25.py in that list? If not, that's your problem. If you don't know how to fix that problem, you have something specific to ask on SO.

Note that "." (that is, the current working directory) is on sys.path. So, running from the same directory as ex25.py will of course fix the problem. Other things on sys.path include:

  • Your system-wide and user-specific (or, if you're using virtualenv, env-specific) site-packages and dist-packages directories, and anything added to that list by the site module. This is how Python packages that you install become usable in all of your scripts.
    • Note that if you just download a file and put it somewhere, rather than following its installation instructions or, e.g., using pip on it, it will not be installed.
  • Any directories that were on the PYTHONPATH environment variable.
    • Note that this is PYTHONPATH, not PATH. Changing PATH (or Path) in the Windows control panel won't affect this, any more than painting all monks yellow affects monkey or chipmunks.
    • Also note that changing things in the Windows control panel often does not affect any currently-open cmd.exe ("DOS prompt") windows, only future ones that you create.
  • Any directories that you explicitly append to sys.path in the script before calling import.

The exact details are more complicated than this, but you really don't want to learn them—at some point you should learn how they work in Python 3.3 and later, but really, nobody wants to know how they worked in 2.7.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • i added path containing ex25.py under Sys Properties/Env Variables/System variables/Path and it still shows there, but when i run "import sys print(sys.path)" it shows me list which has no paths i see under Sys variables – user3141348 Dec 31 '13 at 22:25
  • @user3141348: Well, why did you add it there? Who told you to do that? You want to add it _to your Python `sys.path`_, not to your Windows `%PATH%`. – abarnert Dec 31 '13 at 22:28
  • i followed instructions here http://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7. When i installed python i added folder where python was installed to system variables path in my system settings , then i added folder with all my .py files as path too and it worked. The only problem i had is when i tried to import ex25. I will do more reading i guess – user3141348 Dec 31 '13 at 22:53
  • 1
    @user3141348: You did the same thing as the person asking that question did. You had the exact same problem he did, for the exact same reason. Meanwhile, the answer to that question specifically explains what's wrong—you have to add those paths to `PYTHONPATH`, not to `PATH`. Why would you copy something that explicitly says it doesn't work, instead of copying the corrected version right there on the same page? – abarnert Dec 31 '13 at 22:59