I have a file named task.py with the below code
def add(a,b):
c = a+b
return c
and another file named inputs.py with the below code
import task
a = task.add(2,2)
print(a)
When I run this code, it shows a black output with no errors. Both the files are present under the same directory.
Can anyone confirm why it does not show any outputs?
This worked after i had the Python file and Pycharm file in under C directory.
Python scripts must be in current working directory. You can check CWD using following code:
import os
os.getcwd()
The problem is specific to your system.
I have tried and it worked fine for me
Try the following :
Running again
delete pycache
run by pressing run button in your code editor
run by python inputs.py
Related
I have a directory that contains sub directories of code that I reuse.
MyBaseDirectory
\genericcodedir1
reuse1.py
\simpleapp1
app1.py
app1.py has the following line
import reuse1
Visual studio will fail to run this since it says it can't find the library.
On windows I simply added the genericcodedir1 to the PYTHONPATH environment variable and all is well.
What should I do on the raspberry pi to allow this to run?
error message:
Exception has occurred: ModuleNotFoundError
No module named 'reuse1'
File "/home/pi/Desktop/Mybasedirectory/simpleapp1/app1.py", line 5, in <module>
import reuse1
I assume you have file structure like this and you open Test folder in VS Code As follows.
You can specify the path by adding the following code above the import statement in app.py:
import sys
sys.path.append("./genericcodedir1")
import reuse1
In addition, you can add the following configuration to the settings.json file to make vscode recognize reuse1.
{
"python.analysis.extraPaths": [
"./genericcodedir1"
]
}
code and result
so if your files looks like :
|_genericcodedir
|_reuse1.py
|_simpleapp1
|_app1.py
you need to add an empty file called __init__.py in your genericcodedir.
another note worthy thing is your working directory (the directory in which your terminal runs)
you may need to append to os path depending on where you are when launching the program
In vscode i have two files in the same folder (main.py and helloworld.py), main importing helloworld and using a function from it, but i get the error "NameError: name 'displayText' is not defined" even though it is a function. The code in main.py is
from helloworld import *
displayText()
and the code in helloworld is
def displayText():
print("Hi")
I know it is an error with vscode because i tried testing it in repl and it worked fine.
As you mention you placed two files in the same folder. it is necessary to check that the file name “helloworld.py” and Imported file name are identical. For your concern please check if the two file extensions are ".py". The error message is shown here the program can not find the function 'displayText'.
I try to do the following:
1 VBA script calls shell using
RetVal = Shell(fullpythonexepath & fullscriptpath)
2 Shell get follwing command
fullpythonexepath & fullscriptpath
3 Python Script
import numpy as np
thefilepath = "input.txt" # is in the same folder as the script
theoutputpath = "output.txt" # is in the same folder as the script
ssc=np.loadtxt(thefilepath, delimiter='\t', skiprows=0, usecols=range(2)) #error line
#some other code to fit input data
np.savetxt(theoutputpath, sscnew, header=top, fmt='%1.2f\t%1.2f')
When I run this the output file doesn't get printed, which means the script doesn't run properly.
Now to the funny thing: When I run the python script from IDLE it runs fine. When I run it from the shell using the command:
fullpythonexepath & fullscriptpath
it says :
I tried inputting the fullpath to the input.txt and output.txt. When I do this and run it in the IDLE it does not find the file anymore. When called from the shell it doesn't work either.
Python obviousely does not find the path to the input.txt
the issue is, to my understanding, not related to VBA. The error occures also when using shell commands
Solution was to put :
import os
os.chdir(r"fullpath")
into my script this changes the current working directory to my path and then input.txt gets found.
When you start the script from shell the working directory for the script will be the directory from which it is called upon.
Most IDLE define their own working directory.
To check I suggest doing:
os.getcwd()
in both cases and look what directory is used in both cases
I installed sublime text 2 to OSX 10.8.2.
In my Mac, python 2.7.3 is installed.
In sublime text2, I just type
print 'Hello'
but error occurred like below.
/usr/bin/python: can't find '__main__' module in ''
[Finished in 0.2s with exit code 1]
How can I fix it?
I got the same error as I didn't save the script before executing it. Check to see if you have saved it!
Note to anyone else:
If you have a directory like so, you can add a __main__.py file to tell the interpreter what to execute if you call the module directly.
my_module
|
| __init__.py
| my_cool_file.py # print "Hello World"
| __main__.py # import my_cool_file
$ python my_module # Hello World
You need to SAVE your code file with the ".py" extension. Then, on the 'Tools/Build System' menu, make sure your build system is set to either 'auto' or 'Python'. What that message is telling you is there is no valid Python file to 'build' (or, in this case just run).
First save your program file type as "example.py then run your code its working fine.
did you add the shebang to the top of the file?
#!/usr/bin/python
Don't run with a space between the directory and the filename:
python /root/Desktop/1 hello.py
Use a / instead:
python /root/Desktop/1/hello.py
The problem: The format of the file as to how it is saved.
Use a proper text editor and save it with the .py extension
and run it in terminal.
eg: file name should be saved as `example.py`
run
python example
Save the .py files before you build in sublime.Such as save the file on desktop or other document.
Make sure that you aren't clicking on "Run unnamed" from 'run' tab. You must click on "run ". Or just click the green shortcut button.
Edit the configuration and then in the box: Script path, select your .py file!
You get that error because you haven't saved your file, save it for example "holamundo.py" then run it Ctrl + B
I installed sublime text 2 to OSX 10.8.2.
In my Mac, python 2.7.3 is installed.
In sublime text2, I just type
print 'Hello'
but error occurred like below.
/usr/bin/python: can't find '__main__' module in ''
[Finished in 0.2s with exit code 1]
How can I fix it?
I got the same error as I didn't save the script before executing it. Check to see if you have saved it!
Note to anyone else:
If you have a directory like so, you can add a __main__.py file to tell the interpreter what to execute if you call the module directly.
my_module
|
| __init__.py
| my_cool_file.py # print "Hello World"
| __main__.py # import my_cool_file
$ python my_module # Hello World
You need to SAVE your code file with the ".py" extension. Then, on the 'Tools/Build System' menu, make sure your build system is set to either 'auto' or 'Python'. What that message is telling you is there is no valid Python file to 'build' (or, in this case just run).
First save your program file type as "example.py then run your code its working fine.
did you add the shebang to the top of the file?
#!/usr/bin/python
Don't run with a space between the directory and the filename:
python /root/Desktop/1 hello.py
Use a / instead:
python /root/Desktop/1/hello.py
The problem: The format of the file as to how it is saved.
Use a proper text editor and save it with the .py extension
and run it in terminal.
eg: file name should be saved as `example.py`
run
python example
Save the .py files before you build in sublime.Such as save the file on desktop or other document.
Make sure that you aren't clicking on "Run unnamed" from 'run' tab. You must click on "run ". Or just click the green shortcut button.
Edit the configuration and then in the box: Script path, select your .py file!
You get that error because you haven't saved your file, save it for example "holamundo.py" then run it Ctrl + B