4

I have Python installed on a network drive. I used the pushd command to go into my project's folder on the same network drive. I open python in that folder. I try to import a module which is in that folder. A ModuleNotFoundError is raised:

C:\users\myprofile>pushd \\network\drive\my\directory\my-program
U:\my\directory\my-program>"\\network\drive\Python\installations\python-3.8.3rc1-embed-win32\python.exe"
Python 3.8.3rc1 (tags/v3.8.3rc1:802eb67, Apr 29 2020, 21:21:45) [MSC v.1925 32 bit (Intel)] on win32
>>> import os
>>> import dict2obj
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dict2obj'
>>> os.listdir()
['config.yml', 'dict2obj.py', 'files', 'main.py', 'start.bat', 'templates', '__pycache__']

I utilized os.listdir() to see what files are in my current working directory.

I don't see how this problem has to do with being on a network drive.

Update: Problem persists even after I do the following:

import os
os.chdir("\\\\network\\drive\\my\\directory\\my-program")

Edit: Please do NOT mark this as a duplicate of Python can't find module in the same folder. It is NOT. In that article, OP didn't confirm that their working directory was correct, unlike this issue.

Edit: Here is everything in sys.path:

\\network\drive\Python\installations\python-3.8.3rc1-embed-win32\python38.zip
\\network\drive\Python\modules\site-packages
\\network\drive\Python\modules\custom-modules
\\network\drive\Python\installations\python-3.8.3rc1-embed-win32
a135
  • 119
  • 1
  • 6
  • If you're using the shell, then I don't understand why `sys.path` doesn't have a blank string as the first element. – John Gordon May 14 '20 at 23:53
  • 1
    The embedding distribution is not meant to be used like this. It aims to act as "part of another application" and to be "fully isolated from the user’s system, including environment variables, system registry settings, and installed packages". If you want a distribution that doesn't have to be installed to the system, use the [nuget.org package](https://docs.python.org/3/using/windows.html#the-nuget-org-packages). – Eryk Sun May 15 '20 at 03:57

3 Answers3

2

Solution for me was deleting this file:

python39._pth

This allows Pip to work, and also allow import from same directory. Alternatively you can get this:

https://nuget.org/packages/python

Click "Download package", and you can extract just like a Zip file.

Zombo
  • 1
  • 62
  • 391
  • 407
1
>>> os.listdir()
# can't see __init__.py file here!
['config.yml', 'dict2obj.py', 'files', 'main.py', 'start.bat', 'templates', '__pycache__']
  • Module folder needs __init__.py file to be imported.

check # https://docs.python.org/3/reference/import.html#regular-packages

Ahmed Shehab
  • 1,657
  • 15
  • 24
0

As noted your folder has neither an __init__.py to indicate it is a module nor a __main__.py to indicate a starting point and you are not alternatively specifying from . import dict2obj

Also please share your sys.path so we can see if the current folder is being considered.

Derek S.
  • 348
  • 1
  • 7