13

Python 3.6.5
I am aware of this one: Why does my python not add current working directory to the path? But the problem there is that he's doing something more complicated (referring to a sub-folder but executing from a main folder). The answers there are to either simplify things or to add package definitions.

And the selected answer even says: "It is the script's directory that is added"

However, my problem is really more simple: My script's directory ISN'T added.

Basically, all the tutorials on the internet say: import mymodule
When I do that, I get a name error...

My folder structure:

C:/Projects/interner
    interner.py   # this is the main program body
    aux.py        # this is the auxiliary file I would like to import into the above

I've tried both coding 'import aux' inside interner.py, and also using the interactive console:

cd c:/Projects/interner
python
import aux

To no avail (ModuleNotFoundError: No module named 'aux')

My sys.path:

['C:\\Tools\\Python\\python365\\python36.zip', 'C:\\Tools\\Python\\python365']

(both from inside the script and from interactive console)

Could you please tell me why I can't import local scripts? Is it because my sys.path is missing the PWD? If so, why is it missing it?

Edit: Doing this to help investigation:

>>> import os; print(os.listdir("."))
['aux.py', 'hw.py', 'interner.py', 'my_funcs.py']
  • No, I think it's normal for sys.path to not have the current working directory. It should still get searched during the import process anyway. How about just `import aux`? Does that work any better? – Kevin Jul 18 '18 at 13:23
  • It's `import aux`, not `import aux.py`. The directory containing your code *is* implicitly added to the search path, even if your *working* directory is not. – chepner Jul 18 '18 at 13:26
  • 1
    no, please don't close, the typo is in the question, not my code – Kiichiro Matsushita Jul 18 '18 at 13:31
  • @chepner if that directory is added, shouldn't the sys.path from withing the python code return the current sys.path, ie: I should see the script's directory added there? – Kiichiro Matsushita Jul 18 '18 at 13:38
  • probably duplicate of this: https://stackoverflow.com/questions/38564382/importerror-on-python-3-worked-fine-on-python-2-7 – Jean-François Fabre Jul 18 '18 at 13:41
  • related: https://stackoverflow.com/questions/14216200/force-importing-module-from-current-directory – Jean-François Fabre Jul 18 '18 at 13:43
  • 1
    sorry cannot reproduce. If `aux.py` is in the current directory `import aux` works. Period – Jean-François Fabre Jul 18 '18 at 13:46
  • `cd c:/Projects/interner` doesn't change the drive. Are you sure the current directory is C: ... ? I'd try `cd /D c:/Projects/interner` – Jean-François Fabre Jul 18 '18 at 13:47
  • @Jean-FrançoisFabre, thanks for doing the research. I don't see how the first one is duplicating. I have no packages or PYTHONPATH or __init__.py... I am doing something very simple that everyone says should work but it doesn't and I don't know what's going wrong. The second one is again frustrating to me because it also says: "To ensure that b imports a from its own package its just enough to write the following in the b: import a", which simply doesn't work – Kiichiro Matsushita Jul 18 '18 at 13:48
  • I only have one drive. I am aware of X: to switch drives. – Kiichiro Matsushita Jul 18 '18 at 13:50
  • so basically if you try to import `aux` just after your `os.listdir` what happens ? (sorry if I don't trust what you're saying 100% :)) – Jean-François Fabre Jul 18 '18 at 14:09
  • @Jean-FrançoisFabre (that's fine, I feel like I must be crazy, too!) C:\Projects\SandBox>python Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 >>> import os; print(os.listdir(".")) ['aux.py', 'hw.py', 'interner.py', 'my_funcs.py', 'new 1.txt'] >>> import aux Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'aux' – Kiichiro Matsushita Jul 18 '18 at 14:21
  • have you tried `import hw` ? what happens? or other modules from this dir? – Jean-François Fabre Jul 18 '18 at 14:22
  • @Jean-FrançoisFabre ModuleNotFoundError: No module named 'hw' – Kiichiro Matsushita Jul 18 '18 at 14:28
  • 2
    I believe this is a Python bug, specific to the embeddable (ZIP file without an installer) Windows distribution. I’ve filed https://bugs.python.org/issue34841. – Simon Sapin Sep 29 '18 at 07:50

4 Answers4

10

I believe this is a Python bug, specific to the embeddable (ZIP file without an installer) Windows distribution. I’ve filed https://bugs.python.org/issue34841.

Removing the python37._pth file (presumably python36._pth in your case) from the distribution fixed it for me.

Simon Sapin
  • 9,790
  • 3
  • 35
  • 44
1

I don't know why but it seems that "" is missing from your sys.path variable, and that prevents from importing modules from current directory all right!

I can somehow reproduce your issue (eatcpu.py is in my current dir):

$ python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Windows\\system32\\python27.zip', 'D:\\AppX64\\Python27\\DLLs', ... etc...]
>>> import eatcpu

works. Now in another python session:

$ python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.remove("")
>>> import eatcpu
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named eatcpu
>>>

So the quickfix for you is to do:

import sys
sys.path.append("")
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • This workaround helps. I am now 99% convinced this is because I am using the embeddable version and my installation is not complete (basically, I've only added the python executable location to my windows system path, that's all). If you are interested to know the problem, you can download the embeddable version and then try to reproduce. If you can also think of the missing installation steps, I will drink for you tonight – Kiichiro Matsushita Jul 18 '18 at 15:19
  • it's very possible. That's why noone seemed to believe you. But the fact that "empty string" is missing from the system path is the cause. – Jean-François Fabre Jul 18 '18 at 15:25
0

It looks like you are using the embeddable distribution of CPython rather than one of the regular installers. As described on the documentation page:

The embedded distribution is a ZIP file containing a minimal Python environment. It is intended for acting as part of another application, rather than being directly accessed by end-users.

Since you seem to be directly accessing Python rather than embedding it, you should consider using the regular (or Microsoft Store) installer (also described on the page I linked above).

Zooba
  • 11,221
  • 3
  • 37
  • 40
-2

Try making it explicit:

from . import aux
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 1. Thanks for your answer 2. The suggestion does not work: ImportError: cannot import name 'aux22' 3. I am not really looking for a workaround. I am just learning python (obviously) and if I don't understand something basic like path and import, I will certainly fail in the future – Kiichiro Matsushita Jul 18 '18 at 13:42
  • 1
    Where in your code are you trying to import `aux22`? Is it another module you try to import inside `aux.py`, or is it a typo? – blhsing Jul 18 '18 at 13:45
  • the interner.py code is literally: "import aux". aux22 is indeed a typo, sorry – Kiichiro Matsushita Jul 18 '18 at 13:52
  • And what's in `aux.py`? Where is `aux22` referenced? – blhsing Jul 18 '18 at 13:53
  • sorry, aux22 is just because I was trying different things. I since then deleted it and it's not being referenced anywhere. now it's just these two files: interner.py, which contains: "import aux" and aux.py, which contains: "def printman(): print("Aux is here.")" – Kiichiro Matsushita Jul 18 '18 at 13:58
  • Can you try replacing `import aux` with `exec(open('aux.py').read())` in `interner.py` and see what happens? – blhsing Jul 18 '18 at 14:36
  • it just shows an empty line: >>> exec(open('aux.py').read()) >>> – Kiichiro Matsushita Jul 18 '18 at 14:40