-1

I am using IDLE GUI I created a file called doss.py and it is saved in C:\python34

now on IDLE (GUI)

i want that doss.py to open without any RUN command. in python27 i used

>>> import doss.py 

it worked very fine

bt idk why its showing some LOC in red color.

its :

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    import doss.py
ImportError: No module named 'doss.py'; 'doss' is not a package
1
  • "import doss" instead of "import doss.py" Sep 11, 2014 at 15:46

2 Answers 2

0

You don't have a module called doss.py. Your module, if stored in a file named doss.py, is named doss. So try import doss.

doss.py would be a submodule of doss named py.

1
  • I created 1 more file called boy.py, >>import boy Hello my code execute but again am trying to run the same code its not executing.. moreover whats the problem in doss.py i just gave the name doss.py Sep 11, 2014 at 16:09
0

I'm quite sure that import doss.py would not have worked in Python 2.7's standard shell, at least not in the way that you think. Simply put, import-ing a module is not equivalent to running it from the command line, except inadvertently in some cases. Also, no version of Python has ever accept filenames (e.g. doss.py) as the arguments to import; it has always been a package name (which might be doss if there's a file called doss.py in the current directory, but it gets more complicated).

The standard Python interactive shell has never included a mechanism to do quite what you want: to load and execute a Python script roughly as if from the command line.

You may want to try the enhanced IPython shell instead. IPython does have a %run command that does what you want, in addition to a number of other enhancements.

2
  • My mistake , i tried >>> import hello but why import doss is the error ? the above script am getting when i execute >>> import doss Sep 11, 2014 at 16:14
  • Take a look at the "more complicated" link in my answer above; what import does is rather subtle and intricate. If you simply want to run scripts like from the command line, import is not what you want; use IPython's %run or just run them from the command line.
    – Dan Lenski
    Sep 11, 2014 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.