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
JavaScript
x
14
14
1
# I make some print's to try import and show if it works
2
3
def first():
4
print('Test')
5
6
7
class phrase:
8
def second():
9
print('Hello')
10
11
def third():
12
print('World')
13
14
Code 2
JavaScript
1
7
1
import os
2
3
attempt = os.system(r"python C:UsersGabriPycharmProjectspythonProjectImagens.py")
4
5
# Obviously isn't works =(
6
attempt.first()
7
But in code 2, when i did os.system(r"python C:UsersGabriPycharmProjectspythonProjectImagens.py")
nothing happen.
Someone can help me to import this code? ;-;
1° Code is in C:UsersGabriPycharmProjectspythonProject
2° in C:UsersGabriPycharmProjectspythonProjectPráticaVamove
Advertisement
Answer
If you want to keep the files where they are,
You should by able to do this:
JavaScript
1
6
1
import importlib.util
2
spec = importlib.util.spec_from_file_location(
3
"name", "C:\Users\Gabri\PycharmProjects\pythonProject\Imagens.py")
4
Imagens = importlib.util.module_from_spec(spec)
5
spec.loader.exec_module(Imagens)
6
And then run your commands like this:
JavaScript
1
2
1
Imagens.first()
2