-1

I have a module installed in to my python. I try to run it from CMD and it works but when I run a file with the exact same commands it doesn't work. any ideas? python outcome The top row is when I run the file and the file The bottom row is when I run the command and the program that runs. any ideas?

UPDATE doesn't work also when moved to the same dir: enter image description here

UPDATE 2 Also when I change the file name I get a traceback blaming a drive which doesnt exist (E)

F:\>python env.py
Traceback (most recent call last):
    File "env.py", line 1, in <module>
         import Envyronment.GUI.welcome
    File "E:\Envyronment.py", line 1, in <module>
ImportError: No module named GUI.welcome

UPDATE 3 when I run the code via pycharm I get the following error:

  File "F:/env.py", line 1, in <module>
    import Envyronment.GUI.welcome
ImportError: bad magic number in 'Envyronment': b'\x03\xf3\r\n'
Isdj
  • 1,835
  • 1
  • 18
  • 36
  • 1
    Don't post screenshots. Post actual code, and describe exactly what your error is with it. Currently it is hard to understand your problem – SiHa Jun 16 '16 at 18:57
  • @SiHa the problem is not in the code its in the running of the code – Isdj Jun 16 '16 at 19:04
  • Have you checked [this question](http://stackoverflow.com/questions/514371/whats-the-bad-magic-number-error) for the 'magic number' error? – SiHa Jun 17 '16 at 06:58

3 Answers3

1

The problem is that your script has the same name as the module that you want to import. In the working case, you run python in C:\Users\Isaac\Desktop and then import. Python finds the Envyronment module and everyone is happy.

In the non-working case, you change directories to C:\Users\Isaac and run a script called Envyronment.py. When you import a module, python (quite unfortunately IMHO) checks your local directory for the module before moving on to other python paths. It finds your Envyronment.py script and re-imports it instead of the system script.

You can test this by changing your code to

import os
import Envyronment
print(os.path.abspath(Envyronment.__file__)

The solution is to rename your script. Scripts should not be named the same as modules they want to import.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Already tried. When I run it I get a traceback: File "env.py", line 1, in import Envyronment.GUI.welcome File "E:\Envyronment.py", line 1, in ImportError: No module named GUI.welcome Drive E does not exist so I dont know the problem – Isdj Jun 16 '16 at 19:02
  • You do have a Drive E! Try `cd E:` and it should work. Do you have a USB drive with a copy of your program plugged in? Notice that the system is finding an `Envyronment.py` but that file should be deleted completely. – tdelaney Jun 16 '16 at 19:07
  • E: is an empty drive – Isdj Jun 16 '16 at 19:09
  • Perhaps the file is hidden? Other than that I can't guess whats going on. – tdelaney Jun 16 '16 at 19:20
-1

in the upper window, you are in the "Desktop" directory. In the lower window, you are in your home directory.

Chris Curvey
  • 9,738
  • 10
  • 48
  • 70
-1

Try to put the lib in the same folder of the script and it should work

Raskayu
  • 735
  • 7
  • 20