I need help to import a script, i did 2 codes, the first is a test with some prints and in the second i try to import them:
Code 1
# I make some print's to try import and show if it works
def first():
print('Test')
class phrase:
def second():
print('Hello')
def third():
print('World')
Code 2
import os
attempt = os.system(r"python C:\Users\Gabri\PycharmProjects\pythonProject\Imagens.py")
# Obviously isn't works =(
attempt.first()
But in code 2, when i did os.system(r"python C:\Users\Gabri\PycharmProjects\pythonProject\Imagens.py") nothing happen.
Someone can help me to import this code? ;-;
1° Code is in C:\Users\Gabri\PycharmProjects\pythonProject
2° in C:\Users\Gabri\PycharmProjects\pythonProject\Prática\Vamove
If you want to keep the files where they are,
You should by able to do this:
import importlib.util
spec = importlib.util.spec_from_file_location(
"name", "C:\\Users\\Gabri\\PycharmProjects\\pythonProject\\Imagens.py")
Imagens = importlib.util.module_from_spec(spec)
spec.loader.exec_module(Imagens)
And then run your commands like this:
Imagens.first()
The easiest is if you put both in the same folder and make that folder a python directory from which you can import your other code as modules. All you need for that is a file in the folder containing a blank __init__.py file. Then you can import it to another code file in the same folder using
from folder_name import Imagens
and you should be able to use Imagens functions like any other module
Ex: Imagens.first()
Related
below the folder structure of my software:
below the code of all the .py files:
run.py:
import modules.module_01.aa as a
a.test()
# test:
if __name__=="__main__":
pass
aa.py (module 1):
import libraries.qq as q
import libraries.zz as z
def test():
q.qq_fun()
z.zz_fun()
print("ciao")
qq.py (library used by aa.py):
def qq_fun():
pass
zz.py (library used by aa.py):
def zz_fun():
pass
my question is really simple, why when I run "run.py" Python say to me:
why "aa.py" can't import the module "qq.py" and "zz.py"? how can I fix this issue?
run.py
In run.py, the Python interpreter thinks you're trying to import module_01.aa from a module named module. To import aa.py, you'll need to add this code to the top of your file, which adds the directory aa.py is in to the system path, and change your import statement to import aa as a.
import sys
sys.path.insert(0, "./modules/module_01/")
aa.py
The same problem occurs in aa.py. To fix the problem in this file, you'll need to add this code to the top of aa.py, which adds the directory qq.py and zz.py are in, and remove the libraries. from both of your import statements.
import sys
sys.path.insert(0, "./modules/module_01/libraries")
I have the following 4 python files within the folder SubFolder, assembled as such:
CodeFolder
SubFolder
GeneticAlgorithm.py
main.py
heuristic1.py
heuristic2.py
I am trying to import the file GeneticAlgorithm.py within my main.py file, using the import statement at the beginning of the class:
import GeneticAlgorithm
The problem: PyCharm is highlighting this and says "no module named GA".
Any clues as to what may be causing this issue and how to solve it? Thank you!
Correct me if I'm wrong but you might not have GA module in your GeneticAlgorithm.py
If you do, then you can do similar to below:
Similar to your folder structure:
From main.py call GeneticAlgorithm.py. For example:
from GeneticAlgorithm import GA
def main():
ga_obj = GA(mutation_rate=0.5)
print("Call GA module")
if __name__ == '__main__':
main()
If we look at the GeneticAlgorithm.py
class GA:
def __init__(self, mutation_rate):
self.mutation = mutation_rate
We have GA class.
This is a simple demonstration of how you can use.
I have a very simple script that i wish to test out.
Master script (caller.py):
#!/usr/bin/env python2.7
import test2
test2.printMe()
Function/Module Script (test2.py):
def printMe():
print 'this actually works'
When I run caller.py, it works. Because the test2.py script exists in the same directory from which I'm calling caller.py.
What happens if i need to make the function(s) defined in test2.py available to the caller.py script, through a variable? There are times when the content of the test2.py function script wont always be in a file. Sometimes, it's inside a variable.
So basically, im looking for a way to access the functions that are in a variable just as import would access functions that are in a file.
I wish to be able to do something like this:
from commands import *
def run_command(cmd):
status, text = getstatusoutput(cmd)
print text
run_command('python /tmp/test2.py')
test2.printMe()
Please advise.
Try this:
from sys import path as sys_path
from os import path
def import_module(path_to_py_file):
dir_name, module_name = path.split(path.splitext(path_to_py_file)[0])
sys_path.append(dir_name)
return __import__(module_name)
Now you can do this:
test2 = import_module(r'/tmp/test2.py')
test2.printMe()
I have three files under the same directory, namely, main.py, Newtester.py, and fileUtility.py. In Newtester.py there is a class named Function. In main.py, there are the following codes:
from file.py import *
...
def main():
...
funcs = parseFuncSpec(funcInputFile)
parseFuncSpec is defined in fileUtilities.py as:
some code to import Newtester.py
def parseFuncSpec(fName):
curFunc = function(funcName, numTest, [], score)
Regardless of what I put in import Newtester.py, I always get an error saying "Function" (the class defined in the file "Newtester.py") is not defined. Following Python: How to import other Python files, I have attempted
import Newtester
__import__("Newtester")
exec("Newtester.py")
exec("Newtester")
import importlib
importlib.__import__("Newtester")
os.system("Newtester.py")
But none of them seemed to work. Any advice is appreciated. See https://github.com/r2dong/unitTesting if you are interested in seeing the complete files.
It's because you are not using it correctly
well when you use import statement like below only Newstester file is imported
import Newtester
hence instead of using parseFuncSpec() directly you have to use it as Newtester.parseFuncSpec()
or to use parseFuncSpec() directly you need to use below import statement:
from Newtester import parseFuncSpec
This is not a problem with my code, but rather a general question about Python 3.
Say you had a game, which had 4 parts to it, and the first part (main.py) declares a variable
that say, part 2 needs to run itself. Would you be able to declare that variable, then import part2 (That needs the variable to run smoothly) and have the variable carry on from main.py to part2.py after importing part2.py into main.py.
If you want to use the variable once you can do this.
# part2.py
def scream():
print(sound)
# part1.py
import part2
if __name__=="__main__":
part2.sound = "Yoooo"
part2.scream()
#Output:
Yoooo
If you want to be able to change the variable later. You can create a property
Or simply do this:
# part2.py
# gvars is defined later
def scream():
print(gvars.sound)
# part1.py
import part2
class GameVariables:
pass
if __name__=="__main__":
gvars = GameVariables()
part2.gvars = gvars
gvars.sound = "Yooo"
part2.scream()
gvars.sound = "Whaa"
part2.scream()
#output
Yooo
Whaa
here is a simple example you can try out
file1.py
import sys
sys.argv = "Yellow" #probably not a really great Idea but should suffice
from file2 import sys as sys2
print "Imported argv from file2",sys2.argv
print "Same thing? ", sys is sys2
file2.py
import sys
print "My argv f2:",sys.argv