Python bash script - python

I need help regarding bash scripting as I am new to it.
In the pycharm project I have a file .env.development
It has all the required valriables like-
A=aa ,B=bb ,Python_env=python
Now I need to read this Python_env from the .env.development file (output = python). And I need to use this 'python' in a different file.
The logic which I could think is to create a new .py file and read the .env.development (Python_env) file and output of that file I could use in my master script file.
Please guide me how to do that?
Example:
Consider 3 files-
1)Master.sh 2).env.development 3)Read_env.py
In Read.env file I need a bash script to read Python_Env from .env.development.
After reading the environment I need to use that file in master.sh.
Master.sh has code like --
(Python -m uvicorn)
So python needs to be replaced by some command which is reading the environment variable.

Related

there is a python file where there are variables and it should be changed from another python file, just for some reason it doesn't go

There is a python file where there are variables and it should be changed from another python file, just for some reason it doesn't go
I did it small, which was in my project:
https://i.stack.imgur.com/c4Y2i.jpg

How do force or change my terminal to run python executable file in python 3 just by opening the file?

My terminal is running python 2 so when I run my python file the program fails.
So basically I am new to programming and trying to create a small python script to help me auto create folders. I also want to give the script to colleges so that they can use it on their systems too.
I know that I can run my file in terminal by using "python3 myfile.py" and it will work, but that's too much off a mission to do for my colleges and as my colleges are not familiar with code or terminal for that matter, I wanted to create an executable file so that they just click to open type a few answers to the promoted question and boom folders created.
This is where I run into a problem, I have "#!/usr/bin/env python3" at the top of my file but when I run the script IDLE opens up and it just shows the code I have written but doesn't seem to run the actual script I wrote. Am I doing something wrong?
I also then though perhaps I could just use the terminal to run the file as it is now executable, so I go into terminal and enter "myfile.py" and the program runs but in python 2 so my script fails as it is in python3. So another question would be is there a way to code into my python file, when running this file make sure you use python3? as I would want this to work on all colleges system without them having to write out anything in terminal?
Sorry for the long explanation but any advice would be greatly appreciated.
Thank you in advance
When you are on windows you can just create a .bat file where you write: python3 myfile.py in it.
Both files have to be in the same directory.
If you want to have a .exe you can also use py2exe.
You could also try just #!/usr/bin/python3 without env.
More here.

How to execute Python file

I am learning Python and DJango and I am relatively nub with Linux. When I create DJango project I have manage.py file which I can execute like ./manage.py runserver. However when I create some Python program by hand it looks like that my Linux trying to execute it using Bash, not Python. So i need to write python foo.py instead ./foo.py. Attributes of both files manage.py and foo.py are the same (-rwx--x---). So my Q is: where is difference and how I can execute python program without specifying python? Links to any documentations are very appreciate. Thanks.
You missed one step, after give the corrects permissions to the file, open your foo.py then put this on the first line
#!/usr/bin/env python
Then you can use ./foo.py
I am just gonna add this for more clarity and to anyone coming across this post who might need an explanation.
Why do people write #!/usr/bin/env python on the first line of a Python script?
explains why you should it is used.
Save your python code file somewhere, using "Save" or "Save as" in your editor. Lets call it 'first.py' in some folder, like "pyscripts" that you make on your Desktop. Open a prompt (a Windows 'cmd' shell that is a text interface into the computer): start > run > "cmd".

Blender - Open and parse a .blend file from python script

I would like to open a .blend file from a python script and parse it (get objects, animations and materials).
The documentation I have read so far about how to do this from blender API (running the script as a blender add-on), but I would like to run this script from the command line without opening blender.
I appreciate all the help you can give me.
I realized I don't need to open the binary blender file and parse it in order to use the objects.
Blender has its own python installation, so I put a python script inside the folder path_to_blender/version/scripts/addons , I can execute it in the command line as follows:
blender.exe --background --python ./version/scripts/addons/superScript.py
Next, if you have a .blend file you want to read from your script, put it after the background parameter as follows:
blender.exe --background myFile.blend --python ./version/scripts/addons/superScript.py
And inside your python script do the following:
import bpy
import os
for ob in bpy.context.scene.objects:
print("object name: ", ob.data.name)
In this example I'm printing all the objects inside the scene in the .blend file
As you answered, the easiest way is to use blender, there is also a script that is included with a blender install called blend_render_info.py that extracts the start and end frame settings without using the blender binary, I'm not sure how easy it would be to expand on that but it does work with python 2.x and 3.x
To get more detailed info there is an old project called blender-aid that was created to read and alter blend file data, someone recently had luck using it to extract blend file data using python 2.7 as documented in this question

I have python file name MiscTest.py and now I want to run this file in linux with single name MiscTest only

I am new in python and I have python file name MiscTest.py and now I want to run this file in linux with single name MiscTest only not traditional way like python MiscTest.py.
Can anybody have any idea and also I don't want to right shell script for this ? I know I can do it with shell script
First, you have to make the script executable:
chmod +x MiscTest.py
Next, add this in the first line (the "shebang") of the code:
#!/usr/bin/env python
And make sure that inside the script the execution starts in here (think of this block of code as the main() function in C-like languages):
if __name__ == "__main__":
# call main function
Also, if you need to execute the script from anywhere in the system (without changing directories), don't forget to add the path of the script to the $PATH environment variable of your shell, see this post for details.
By doing all of the above, you won't have to call the script like python MiscTest.py, it'll be enough to say MiscTest.py in the shell, or to double-click it in a file explorer. As a side note, although you can, I believe you shouldn't remove the .py extension of the file, that's how it's recognized as a Python file by the different tools and programs in your system.
To chmod the "MiscTest.py" file so that the owner can read, write, and execute the file, the group can read and execute the file, and the world can execute the file, issue the following command:
chmod 751 ~/MiscTest.py

Categories

Resources