How to call functions from other python files [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am working on a Python Project and want to call multiple functions from other python files, but I'm having trouble. Can someone please help me?

Each of your Python file can act as a module, that you can import.
for example for numpy you just type 'import numpy' and then you'll be able to use numpy.sin() function.
by default you can only append modules located in sys.path. So What you can do is:
import sys
if not (<folder_with_your_function> in sys.path):
sys.path.append( <folder_with_your_function> )
import <your_function>

Related

How to debug with exec in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
I have a file with the code and I want to run it using exec(file.read()). However when I put the breakpoint in that file then it's not reached. How can I run this file and make the breakpoint work?
That's not the usual way of running Python files, but if you have a breakpoint() in the middle of the file, it should work.
I think you actually want to import the file or run it directly.

How do I open an application using Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How do I open an app like Minecraft using os or subprocess? I went through a few videos and none of them worked. Do I just use the name of the app or its location?
For this example, you could use:
subprocess.run(r'"C:\path_to_minecraft\minecraft"',shell=True)
Or if you have Windows shortcuts you add the .lnk extension in the call:
subprocess.run(r'"C:\path_to_minecraft\minecraft.lnk"',shell=True)
this works.
import os
os.system("your app.exe")

Access modules from Python in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to access a module I have written in Python from C? Is this at all possible?
I have tried:
Module = PyImport_ImportModule("<modulename>");
But it doesn't seem to work
please try to add more information of what are you trying to do.
Anyway, I know it is possible. It isn't easy though..
Have you tried looking for any tutorials? I think this was already asked:
Call a Python function from within a C program
https://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I
https://www.linuxjournal.com/article/8497

How to use breakpoint in python without using IDE features [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
It will great help if you can explain with code example or any useful resource.
you can use the in-built pdb library.
import pdb
a = 10
pdb.set_trace()
print(a)
So what it would basically do is, stop the program after line 2 is executed. and from there you would have command line access to the code with is execute above the set_trace() is added.
You can read more about it here.
The breakpoint() method is part of the python language now. See: https://www.python.org/dev/peps/pep-0553/
If you are using an earlier version of python then you can have the same functionality by importing pdb by import pdb at the top of you file and pdb.set_trace() where you want to be able to inspect variables.
When you are dropped into the pdb shell you type h to see what commands are available.

Can you use python to load a quick ruby script? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was curious if you could use python to load a quick ruby script? then continue with python.
The simplest way is to use call from subprocess module:
from subprocess import call
call(['ruby', 'quick_script.rb', 'some_parameter'])
Ruby should be available from the path variable.

Categories