2

I am creating a script that will call an API and return some results. I have the script working with pycharm on my computer but I am running into a few problems but I want to focus on this problem first.

1) I am unable to set Python3 as my default python.

I am using a Mac. When I go into terminal I enter $ python --version and it returns Python 2.7.10

I then enter $ alias python=python3, and when I run $python --version it returns Python 3.7.2

When I create a py.script with the os module, it does not work. See my code below.

import os
os.system('alias python=python3')
print(os.system('python --version')

It prints 2.7.10

I also tried to run the os.system('alias python="python3"')

2
  • 3
    You must not set Python 3 as your default (system) Python. Doing so could break your OS in all sorts of ways as it has many scripts that rely on using Python 2. Why not change your question to be about what you are trying to do, rather than how you are trying to achieve it. That is, why do you want Python 3 to be your default? Commented Feb 11, 2019 at 16:29
  • what I am trying to do is run an api call via the Requests module, but when I run the script the Request module is not found (unless I run it via Pycharm). However, if I open IDLE and import Requests manually it works, I just want to save it as a script. Here is the code import os import requests #perform actions. When I save it as a .py and attempt to run the script via Python Launcher I get an error message saying ImportError: No module named requests Commented Feb 11, 2019 at 16:58

4 Answers 4

3

On -nix machines (including OSX), one way to change the version of the interpreter that the script runs with is to add a shebang as the first line of your script.

Eg.

#! /usr/bin/env python3

import sys
print(sys.version)

Then to run your script do:

~/$ chmod u+x myscript.py
~/$ ./myscript.py 

You only need to run the chmod command the first time. It enables you to execute the file. Whenever you run your script directly (rather than as an argument to python) your script will be run using the version specified by the shebang.

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

Comments

2

welcome to SO! Pycharm needs you to specify which interpreter to use as default, as it wouldn't choose the system one by default.

So if you want python3, you can run which python3, and use the path as a settings for the current project. How to do that step by step is here:

https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html

Hope it help, post a comment if you need more details.

1 Comment

I can get the script to run on pycharm exactly as I want, I just can't get the import requests module to work when I save it as a .py, I get the error ImportError: No module named requests when I attempt to run it via Python Launcher
1

This isn't surprising, because os.system opens its own shell, and running alias in that way only affects the currently running terminal. Each call to os.system would be in a separate shell.

I'm not sure what your ultimate goal is, but you almost certainly don't need to change what python means to a shell to do it. If you DO, you'll have to run both commands at once.

import subprocess

cp = subprocess.run("alias python=python3 && /path/to/script")

6 Comments

Ultimately, I want to create a script that can be used by multiple users, it needs to run an API call to Postman via the requests module. So, I am still in the testing phase. I can get the script to run on my machine via PyCharm no problem, but when I save it as a .py and try to run it via the Python Launcher I receive the following error message: "ImportError: No module named requests"
That sounds like a whole different problem. You can over-engineer a solution using setuptools's entrypoints directive and building the application into a built package, then distributing that bdist or wheel file to each user. It's a lot more work, but it has the benefit of handling all your dependency management by itself.
@CaliforniaDataSurfer note that if you put #!/usr/bin/env python3 as the first line of your Python file, it should just work.
Do you mean to run something like this #!/usr/bin/env python3 import requests
No, put that line (called a shebang) as the first line of your python script. Then double-clicking it from the Finder should use python3 rather than python.
|
0

Interesting - apparently os.system ignores the alias? Just checked it in Linux and got the same results.

Try sys instead of os:

import sys
print(sys.version)

1 Comment

via terminal I get 2.7.10, via IDLE I get 3.7.2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.