Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing modules issue
#1
Hi,

I have a python file called ImportingModules.py

I have saved this file purposely on my desktop because I want to see how you can add an Environmental Variable called PYTHONPATH and set this equal to the path where ImportingModules.py is saved (The path is C:\Users\mp88_\OneDrive\Desktop). Then I want to import this file in another module.

When i try doing this using this one line code:

import ImportingModules.py
I get an error saying "No module named ImportingModules"

I am using Windows 10 Home and executing the code using Visual Studio 2019.

Why am I getting this error?

Thank You
Reply
#2
Does it really say "No module named ImportingModules" or "No module named ImportingModules.py"? You don't want to put the ".py" on the import line.
import ImportingModules
Reply
#3
Hi,

Sorry, I removed the ".py" from "import ImportingModules.py" so it now reads "import ImportingModules".

Still get an error saying "No module named ImportingModules"

I've also added __init__.py file in the directory where I am trying to import from as some threads have suggested that.

But still no luck
Reply
#4
Your procedure sounds correct, and works fine for me on my linux box. I won't be able to validate it on Windows for a while. How did you set PYTHONPATH?

You don't need an __init_.py. You can import bare modules without it.
Reply
#5
I set up my PYTHONPATH variable as follows:

1). Goto my pc
2). Properties
3). Advanced System settings
4). Then click on Environment Variables
5). Then click on New under "User Variables for mp88_". There is also System variables too but I added it under User Variables.
6). PYTHONPATH = C:\Users\mp88_\OneDrive\Desktop
Reply
#6
Have you restarted your PC since adding the PYTHONPATH?
Reply
#7
Yes, I have restarted my pc since adding PYTHONPATH
Reply
#8
Restart as mention.
Then start python repl from command line,to test that it work.
C:\
λ python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>>
>>> os.environ['PYTHONPATH']
'C:\\bar'
Here i have set PYTHONPATH to C:\bar as a test.

Look at this post for an other way not using PYTHONPATH.
Reply
#9
in command prompt, os.environ['PYTHONPATH'] prints 'C:\\Users\\mp88_\\OneDrive\\Desktop'
Reply
#10
Check that path and file is really working from cmd,you know how to navigate and look at files from cmd cd and dir?
Example with more to open a file on Desktop.
C:\>more C:\Users\Tom\Desktop\intro.py
print('hello world')
cmd/Powershell is terrible as i use cmder,had to search for how to open a file more as i had forget it,
in cmder is like a normal way with cat Wink
C:\
λ cat C:\Users\Tom\Desktop\intro.py
print('hello world')
Desktop is not good folder for storing files,make folder with simpler path like eg C:\code
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  confusion on importing modules carter187 4 411 Mar-03-2024, 08:11 PM
Last Post: snippsat
  Importing modules from different folders Tomli 3 1,464 Jun-26-2022, 10:44 AM
Last Post: snippsat
  Importing Custom Modules in Python 3 Flexico 1 2,577 Aug-24-2019, 08:11 PM
Last Post: snippsat
  Trouble importing modules on a new python version snackman_barry 2 2,564 Jul-12-2019, 11:15 AM
Last Post: snackman_barry
  importing modules PiaNa 1 1,957 Jun-24-2019, 12:50 PM
Last Post: ichabod801
  Importing all modules and using it rohitnirantar 2 2,545 Aug-28-2018, 08:15 PM
Last Post: snippsat
  Importing modules SBachar 2 3,044 Apr-06-2018, 09:08 PM
Last Post: snippsat
  Importing modules Pistolpete 2 2,703 Nov-29-2017, 05:24 PM
Last Post: nilamo
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,429 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020