admin管理员组

文章数量:1333661

I am new to Python and still trying to get past something more practical than copy&paste (and I do search for answers and try everything suggested before posting a question)... I have tried Laincher, VSC, Jupyter, and all either have problems each is different) or the graph plot when displayed loses the tool bar. Copy script ( RGB_plot.py ) and paste into a Python shell in terminal and works quickly and without issues.

I am trying make it executable so I can convert to .app and run from a desktop icon (and uultimately in the background... but one thing at a time)

After looking at a number of answers about making executable, I have inserted the following at the first lines:

#!/usr/bin/env python

from os import chmod

chmod +x RGB_plot.py

Initially it was only the top and bottom line. bottom line started with $ which resulted in "\u24" error, I added the middle line "from os import chmod" which eliminated "chmod not defined Pylance.... but I get the same not defined for 'x' and RGB_plot.py (perhaps because it is also the name of the script???)

Here's the script

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

sns.set_style("whitegrid", {'axes.grid' : False})


df = pd.read_excel('/Users/stanbrown/Desktop/Plot/RGB     plots.xlsx')
print(df.head)
print(df.columns)

fig = plt.figure(figsize=(6,6))
ax = plt.axes(projection='3d')

ax.set_xlim(left=0)
(0.0, 1.0)
ax.set_ylim(0.0, 1.0)

ax.set_zlim(bottom=0)
(0.0, 1.0)

for i in range(len(df)):
ax.scatter3D(df['R'][i], df['G'][i], df['B'][i], 
             marker=df['M'][i], c=df['C'][i], alpha=0.5)

ax.set_xlabel('Red')
ax.set_ylabel('Green')
ax.set_zlabel('Blue')

plt.show()

I am new to Python and still trying to get past something more practical than copy&paste (and I do search for answers and try everything suggested before posting a question)... I have tried Laincher, VSC, Jupyter, and all either have problems each is different) or the graph plot when displayed loses the tool bar. Copy script ( RGB_plot.py ) and paste into a Python shell in terminal and works quickly and without issues.

I am trying make it executable so I can convert to .app and run from a desktop icon (and uultimately in the background... but one thing at a time)

After looking at a number of answers about making executable, I have inserted the following at the first lines:

#!/usr/bin/env python

from os import chmod

chmod +x RGB_plot.py

Initially it was only the top and bottom line. bottom line started with $ which resulted in "\u24" error, I added the middle line "from os import chmod" which eliminated "chmod not defined Pylance.... but I get the same not defined for 'x' and RGB_plot.py (perhaps because it is also the name of the script???)

Here's the script

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

sns.set_style("whitegrid", {'axes.grid' : False})


df = pd.read_excel('/Users/stanbrown/Desktop/Plot/RGB     plots.xlsx')
print(df.head)
print(df.columns)

fig = plt.figure(figsize=(6,6))
ax = plt.axes(projection='3d')

ax.set_xlim(left=0)
(0.0, 1.0)
ax.set_ylim(0.0, 1.0)

ax.set_zlim(bottom=0)
(0.0, 1.0)

for i in range(len(df)):
ax.scatter3D(df['R'][i], df['G'][i], df['B'][i], 
             marker=df['M'][i], c=df['C'][i], alpha=0.5)

ax.set_xlabel('Red')
ax.set_ylabel('Green')
ax.set_zlabel('Blue')

plt.show()
Share Improve this question edited Nov 20, 2024 at 18:17 Stainless Brown asked Nov 20, 2024 at 16:54 Stainless BrownStainless Brown 95 bronze badges 1
  • 1 try running chmod +x RGB_plot.py from your shell not from within the python script. You likely also don't need the import then. – JonSG Commented Nov 20, 2024 at 17:30
Add a comment  | 

1 Answer 1

Reset to default 0

Save your Python script on your Desktop as RGB_plot.py.

Now start Terminal and change the mode of your script to be executable by running:

chmod +x $HOME/Desktop/RGB_plot.py

You only need to do that once.

Now you can execute, i.e. run your script by typing the following into Terminal:

$HOME/Desktop/RGB_plot.py

If you are already in your HOME directory, you can just use:

Desktop/RGB_plot.py

If it still doesn't work, check that you have saved your script properly and not as RTF by running the following in Terminal:

file $HOME/Desktop/RGB_plot.py

and add the output to your question, along with the full Python script.

本文标签: python 3xHow to make py file executable (OSX)Stack Overflow