1

When I write python music_player.py in the windows command prompt the file runs successfully. Image

But When I try the same with the music_player.py shortcut file python "music_player.py - Shortcut.lnk" it gives me this error:

Traceback (most recent call last):
  File "E:\Music\music_player.py - Shortcut.lnk", line 1, in <module>
NameError: name 'L' is not defined

Image


Also if I try to open a .py file with Python then it shows the same error.

enter image description here

But now I uninstalled Python2 and also restarted my PC.


So my questions are:

  1. If I write python music_player.py in Command Prompt it will run successfully, But if I try the same with the music_player.py shortcut file it shows me this error
Traceback (most recent call last):
  File "E:\Music\music_player.py - Shortcut.lnk", line 1, in <module>
NameError: name 'L' is not defined
  1. If I right-click on any Python file and use Open With Python I get the same error.

Note:

  1. This all happens since I installed python2.
  2. All code is written in Python3
  3. My code work in vscode and Thonny.
  4. All Code Is Available On My Github Repository(Too Many Lines Of Code).
codester_09
  • 5,622
  • 2
  • 5
  • 27
  • 1
    can you provide some code to help us figure it out this problem. – LSeu Mar 04 '22 at 16:37
  • @LSeu As I say There Is no error in my code. – codester_09 Mar 04 '22 at 16:39
  • 1
    Ok my bad i may understand why your music_player is not running. Since you just double click on file from windows you need to create a batch file to give the system a python interpreter : go see this [link](https://stackoverflow.com/questions/37219045/windows-run-python-command-from-clickable-icon) – LSeu Mar 04 '22 at 16:43
  • @LSeu This not my question – codester_09 Mar 04 '22 at 16:44
  • @LSeu This is the GitHub link to my repository:https://github.com/Sharimiqbal/music-player – codester_09 Mar 04 '22 at 16:46
  • There you get the code – codester_09 Mar 04 '22 at 16:46
  • There is no actual problem here, you just misunderstand how to call a python script with parameters, the quotes are not necessary. Your current python call is passing the .lnk file as a module, which is not correct. – Dr. Snoopy Mar 04 '22 at 17:59

1 Answers1

2

Although whenever you double click to run that shortcut, it will redirect you to the original Python file, that shortcut file itself is not a module.

In windows command prompt you can use type command which displays the contents of a text file. (It is not a readable text file though)

enter image description here

As you see it has necessary information to redirect you when you double click on it. Then OS will execute the referenced file by the specified app which is python.exe in your case.

But Python cannot treat it like a regular Python module. It will inspect the content of that file which is showed by type command.

When you type, python something, it doesn't matter what file extension it has, interpreter reads the content, compile the content to create pyc file and then interpret that pyc file.

If you use -m option, Python can execute the original source file but after that it will give you the same error.As @martjin said here:

When you use the -m command-line flag, Python will import a module or package for you, then run it as a script. When you don't use the -m flag, the file you named is run as just a script.

So in the process of importing Python somehow resolves the source file's path and import the file. This is when the file is being executed(Modules are executed when they get imported). After that it will fail on running the actual shortcut file's content.

One solution is to execute it like this:

python -c "import os;os.startfile('p_file.py - Shortcut.lnk')"

So you are basically calling os.startfile:

When operation is not specified or 'open', this acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated.

S.B
  • 13,077
  • 10
  • 22
  • 49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242615/discussion-between-sharim-iqbal-and-soroush-bakhtiary). – codester_09 Mar 05 '22 at 06:48