I am a novice and I am trying to follow this code: https://github.com/llSourcell/autoencoder_demo/tree/master
Now, as you can see in the picture, the main file throws a no module found error. The question I have is how can I import the input_data found in the adjoint file?
You can do the following to fix the problem.
Extract the files to some folder. Open a command prompt from that folder and then run the script. Make sure that you run cmd from the directory where files are. This is because, python looks at the current directory and Python path, when you try to import a module.
Related
My program is located under C:\Users\Username\pycode.py
My file to be called within the program is located under C:\Users\Username\<filename>
The file and the code are in the same directory.
When I call from within the program using the below set of codes,
import os; import sys
with open(os.path.join(sys.path[0], "file1.txt"), "r") as fn1:
print(fn1.read())
the Py code picks up the file and executes the applied methods on it.
However, when I don't use the above code and use as:
fn1 = open("file1.txt", "r")
print(fn1.read())
There is only an error that the file does not exist.
Any thoughts and ideas, what is missing to make the file within the local directory as the py code to recognize the file.
If it's relevant, I'm a beginner using VSCode on Windows 10.
Speaking from personal experience, it happens to me when I am using vscode and I am in a directory say d:/Desktop/python programs. But the file I want to open() is in the directory d:/Desktop/python programs/fun.
In this case it is not sufficient for me to use the path like "filename". Instead I need to use "/fun/filename". This is even though the file I am working on is in the fun folder.
So perhaps you could go to the terminal in VSCode (your IDE) and run cd "path to your file" and that should solve the problem
Try running the following code and see what output you get
import os
print(os.getcwd())
In my case this would print d:/Desktop/python programs
So I have a python script with command line arguments that works fine on my computer.
I run it from a bat like this:
Set PYTHONPATH=Folder1;Folder2
Set File=%~1
python %File% --arg1=value --arg2=value2
I want to deliver it to someone who does not have Python, I figured I would put the whole Python directory in the delivered archive.
Problem is, if I replace "python" in the above code by the path of the python.exe I put in the archive along with the rest of the required python files, it can't import the py files in Folder1 and Folder2. I guess the PYTHONPATH changes I do at the beginning of the bat file are ignored because if I print sys.path at the beginning of the script I'm trying to run, Folder1 and Folder2 are missing.
I don't think I can use pyinstaller because actually the py script I'm talking about is edited by the person who receives the archive through another tool that has nothing to do with Python. The py itself is an input to the bat file and requires several libraries that I can't load because there are in Folder1 and Folder2 which are not seen by the python exe in the archive. I can't make an exe out of the script file because it is supposed to be edited by the user.
I'm a beginner with Python, so I'd appreciate it if you could tell me what is going on (and also how to fix the import module problem).
First of all, I am really a python idiot, and this is my first python test.
I am running a test_predictors.ipynb file using Jupyter. I ran into an "ImportError: No module named" error when executing the test_predictors.ipynb file by block, like following:
The decisioni_tree.py is another .py file in the same folder as test_predictors.ipynb. calculate_information_gain, decision_tree_train, decision_tree_predict are all functions defined in decisioni_tree.py. The following picture is showing the file layout:
I searched a lot of threads, tried putting all .py files into a subfolder, or adding a leading dot in front of decisioni_tree, or adding full path to decisioni_tree, but none of these worked.
I also read PEP, but it does not make much sense to me. Now I am really clueless. I guess it was the path problem, but I don't figure out the logic behind how python arrange their path. I am wondering anyone can give some pointers? How should I solve this problem? Thanks.
I am using Windows 10, with I think Python 3.4/3.5 installed.
Try this:
sys.path.insert(0, 'directory_of_the_pyfile_you_want_to_import')
import FOLDER.file
where FOLDER is the module name and file is the .py file you want to import.
This if the __init__.py solution don't work..
Seems to me that you cannot import any script from your working directory.
This shouldn't be happening.
I suggest you take a look at your python paths by running in your notebook:
import sys
print sys.path
if your home directory or directory of notebooks, something like 'C:\\Users\\yourusername\\.ipython' doesn't show up then you probably need add it as a path.
try adding your home directory to path:
sys.path.append('C:\\Users\\yourusername\\')
or
sys.path.insert(1, 'C:\\Users\\yourusername\\')
I made a program using the pygame module on Python 3 and it works fine within python, but when I try to compile is using py2exe it won't run. (I just get the programName.exe has stopped working error upon trying to run it).
I managed to narrow down this problem to the pygame.font module as when I comment all the lines that use that module everything works fine. I tried to forcefully include the module using the -i flag in py2exe, but it doesn't appear to change anything...
What am I doing terribly wrong?
Edit: I managed to get the reason of the program not working - it crashes as it can not find build\executable.exe\pygame\freesansbold.ttf . What I don't understand is why the hell is the pygame folder supposed to be located in a folder with the name of my executable? (Of course, I can not create a folder with the same name as an existing file in the directory). If anyone has a clue to how to fix it, please help!!
I had the same problem using cx_Freeze, so hopefully this will work for you as well. Open up your pygame package folder. It should be C:\Python34\Lib\site-packages\pygame. There should be a True Type Font File titled freesansbold.ttf. Copy that file then open the folder containing your exe program. There should be a zipped file called library. Open it up and go to the pygame folder inside the zipped file. Should look something like this \build\exe.win32-3.4\library.zip\pygame. And just paste the freesansbold.ttf file in that folder and it should work perfectly.
I managed to find a way! By including -l library.zip argument in the build_exe command and then following the instructions given by DeliriousSyntax in the answer above I managed to get it to work!
I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave