0

I feel like this is incredibly easy to fix, but for some reason it isn't.

I want to run a program in linux that opens python file filename.py by writing:

python3 filename arg

but it only works if i write:

python3 filename.py arg

Is there an easy way to run it without adding the extension? And without removing the extension completely? I wouldn't have imagined this to be a problem at all, but here we are. Thankful for help!

3
  • What error message do you get? Commented Mar 15, 2018 at 14:03
  • 2
    Why? Also, what if there's a file named "filename" (without .py) in the same directory? Also, does python3 -m filename arg solve your problem? Commented Mar 15, 2018 at 14:04
  • python3: can't open file 'isConnected': [Errno 2] No such file or directory I'm certain I'm in the right directory though! Commented Mar 15, 2018 at 14:05

2 Answers 2

1

Your file is called filename.py, therefore you call it with python3 filename.py. If you want to call it with python3 filename, you'll need to rename the file.

The one thing you can do is call Python with the -m switch, which will try to import a module with that name, i.e. python3 -m filename. That should work without renaming the file.

Sign up to request clarification or add additional context in comments.

1 Comment

Hah. I'm guessing there's something wrong with my assignment then! The requirements state that what i described should be possible but after all the time I've spent on it I feel like you're right. Guess there's a typo or something somewhere! Thanks for the clarification!
1

Firstly, if you start your python script with the shebang, then you won't need to type 'python3' before the filename each time you wish to run the code.

`#!/usr/bin/env python3'

Secondly, if you create a setup.py file is the same directory. Then you can install your script locally using: $ sudo pip3 install -e . from within your directory.

You will then be able to run your script from anywhere within terminal using: $ myscript args

More info on creating a setup file here: https://python-packaging-user-guide.readthedocs.io/tutorials/distributing-packages/#setup-py

and example setup file here: https://github.com/pypa/sampleproject/blob/master/setup.py

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.