Kiichiro Matsushita
Kiichiro Matsushita

Reputation: 151

Why is my script's directory not in the Python sys.path?

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']

Upvotes: 13

Views: 8282

Answers (4)

Zooba
Zooba

Reputation: 11438

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).

Upvotes: 0

Simon Sapin
Simon Sapin

Reputation: 10180

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.

Upvotes: 13

Jean-François Fabre
Jean-François Fabre

Reputation: 140148

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("")

Upvotes: 1

blhsing
blhsing

Reputation: 106445

Try making it explicit:

from . import aux

Upvotes: -2

Related Questions