can not import a python program in the same folder
C:\Users\User_Unknowed\Desktop\Marvin
So i have started to just look if it where the fun.py that could be wrong or it can't just be find which now print error so i need help.
My code on test67.py:
start of code
import os.path
if not os.path.exists(fun.py):
print("Alarm")
if os.path.exists(fun.py):
print("import")
end of code
Anything of an solution would be lovely. Answer as quickly as possible please, thx.
To import a file inside of the same directory use:
import fun
To locate the file in a specified directory use os.walk:
import os
def find(nameofFile, path):
for root, dirs, files in os.walk(path):
if nameofFile in files:
return os.path.join(root, nameofFile)
if you find this file is not in the same directory you can use sys.path.insert() to add the path of the file you want to import:
import sys
sys.path.insert(0, pathtoFile)
from fun import *
Hope this helps.
Related
When I try to import my Function from a lower directory it doesn't work.
It works if both file's are in the same directory but if they are not, it fails saying there is no Module named "GameFunctions"
So this would work
./game.py
./GameFunctions.py
But this wouldn't
./etc/game.py
./GameFunctions.py
I tried using the code
from GameFunctions import * when both were in the same directory. it worked
Tried the same thing while they were in different directories
but I got the error
Traceback (most recent call last):
File "d:\Projects\MyScripts\PYTHON\Test\tes\game.py", line 1, in <module>
from GameFunctions import *
ModuleNotFoundError: No module named 'GameFunctions' ```
It can apparently read the file though, since it causes a problem if I rename a def in GameFunctions, it says the def is not defined in game.py
Ohk a much easier solution do one thing write your input statement like this
from parentdirectory.GameFunctions import *
This should solve your error
you can add folder of GameFunctions.py file to the python path:
import sys
sys.path.append('/Path/to/GameFunctions-Folder')
from GameFunctions import *
My folder structure is as follows:
In my App.Js (which is under the components folder), I have:
import variables from '/src/EnvVariables/variables.json';
However, I get an error:
You attempted to import /src/EnvVariables/variables.json which falls
outside of the project src/ directory. Relative imports outside of
src/ are not supported. You can either move it inside src/, or
add a symlink to it from project's node_modules/.
I have also tried:
import variables from './src/EnvVariables/variables.json';
import variables from '/src/EnvVariables/variables.json';
import variables from '/src/EnvVariables/variables.json';
import variables from '/EnvVariables/variables.json';
import variables from './EnvVariables/variables.json';
All either give the above error, or say "can't resolve file".
This file is in a folder that is under /src, as they said. But it still tells me it must be under the /src/ directory. How can I import my variables.json file? I don't want to just put it under "components", i prefer to better organize it.
Have you tried to import with the relative path? (with one or more ../ sections)
import variables from "../EnvVariables/variables.json"
Of course, if you prefer absolute paths you can setup https://webpack.js.org/configuration/resolve/, but I believe for first, you should try the first solution
Do ../../ before path
source={require('../../assets/eating.png')}
if you just need to import your any file to a component : (just follow this steps)
Simply you can create a .env file in src directory .
In that .env file you need to add this line only NODE_PATH = src/
Then you can write import "Home.css" (your file name will be here) to a component from src, it will take it easily.
Run the program again , then you will get the same error, but take
it easy and just you have to do fully start your program with the npm start from
a new terminal.
Seems like I can not import numpy properly from within C application which embeds python, when using local .zip python file containing all the python files and binaries rather then standard python installation. This is the error I'm getting :
zipimport: zlib available
python27.zip\Lib\site-packages\numpy_globals.pyc has bad mtime
zipimport: zlib available
import numpy._globals # loaded from Zip python27.zip\Lib\site-packages\numpy_globals.py
zipimport: zlib available
python27.zip\Lib\site-packages\numpy__config__.pyc has bad mtime
zipimport: zlib available
import numpy.config # loaded from Zip python27.zip\Lib\site-packages\numpy__config__.py
zipimport: zlib available
python27.zip\Lib\site-packages\numpy\version.pyc has bad mtime
zipimport: zlib available
import numpy.version # loaded from Zip python27.zip\Lib\site-packages\numpy\version.py
zipimport: zlib available
python27.zip\Lib\site-packages\numpy_import_tools.pyc has bad mtime
zipimport: zlib available
import numpy._import_tools # loaded from Zip python27.zip\Lib\site-packages\numpy_import_tools.py
zipimport: zlib available
python27.zip\Lib\site-packages\numpy\add_newdocs.pyc has bad mtime
zipimport: zlib available
zipimport: zlib available
import math # builtin
zipimport: zlib available
import numpy.lib.info # loaded from Zip python27.zip\Lib\site-packages\numpy\lib\info.pyc
zipimport: zlib available
zipimport: zlib available
zipimport: zlib available
python27.zip\Lib\site-packages\numpy\core\info.pyc has bad mtime
zipimport: zlib available
import numpy.core.info # loaded from Zip python27.zip\Lib\site-packages\numpy\core\info.py
import numpy.core # loaded from Zip python27.zip\Lib\site-packages\numpy\core__init__.pyc
import numpy.lib.type_check # loaded from Zip python27.zip\Lib\site-packages\numpy\lib\type_check.pyc
import numpy.lib # loaded from Zip python27.zip\Lib\site-packages\numpy\lib__init__.pyc
import numpy.add_newdocs # loaded from Zip python27.zip\Lib\site-packages\numpy\add_newdocs.py
import numpy # loaded from Zip python27.zip\Lib\site-packages\numpy__init__.py
cannot import name multiarray
But when I extract abovementioned .zip file, and run python.exe interpreter and import numpy, everything works fine.
I've build python 2.7.13 from source, x86 Release. After that installed numpy-1.11.3+mkl-cp27-cp27m-win32.whl file from here, then made a python .zip archive with all neccessary files following common folder structure.
This is how my C code looks like :
int main(int argc, char **argv)
{
Py_VerboseFlag++;
Py_NoSiteFlag++;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(".");
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path = ['.','python27.zip','python27.zip/DLLs','python27.zip/Lib', 'python27.zip/Lib/site-packages']");;
PyRun_SimpleString("print sys.path");
printf("\n");
char filename[_MAX_PATH];
_fullpath(filename, "mod1.py", _MAX_PATH);
PyObject* main_module = PyImport_AddModule("__main__");
PyObject* main_dict = PyModule_GetDict(main_module);
PyObject *pyFileObj = PyFile_FromString(filename, "r");
if (pyFileObj == NULL) {
return -1;
}
FILE *pFile = PyFile_AsFile(pyFileObj);
if (pFile == NULL)
return -1;
PyObject *result = PyRun_File(pFile, filename, Py_file_input, main_dict, main_dict);
if (!result)
print_error();
printf("\n\n");
Py_Finalize();
getchar();
return 0;
}
module mod1.py, I'm executing contains this :
import time
import json
import numpy
if __name__ == "__main__":
print 'Success'
I'm aware that that error comes from the fact that python can not load multiarray.pyd which is shared library.Do I need to handle this scenario separately when it comes to import?
Is it possible to import numpy properly from zipped, non-standard python installation from C application?
EDIT : Forgot to mention following :
- My OS is Windows 10 x64 Version 1511 (OS Build 10586.545)
- When I extract .zip archive in my Release folder where my C app binary resides, everything works fine, numpy is loaded properly with following sys.path
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path = ['.', 'DLLs', 'Lib', 'Lib\\site-packages', 'Sources']");
PyRun_SimpleString("print sys.path");
EDIT 2: Also, thought to check exact version of python27.dll the multiarray.pyd is linked to (according to other posts about similar problems) and it turned out it is different then mine - it's 2.7.8. No problem, extracted specific python27.dll v2.7.8 binary from the .msi package and replaced mine which was 2.7.13, but still no luck. It is definitely something about zipimport module.
The best way is probably to unzip it for now. The zipimport module is what Python uses to load modules from zip files and unfortunately it disallows the import of dynamic compiled code (probably due to security concerns, consistent with PEP 273):
Any files may be present in the zip archive, but only files *.py and
.py[co] are available for import. Zip import of dynamic modules (.pyd, *.so) is disallowed.
Because the wheels provided will be platform wheels, you will also need to remove the platform extension from the .so filenames which are added by Cython as part of adherence to PEP 3149. I.e., foo.cpython-XYm.so would need to be renamed to foo.so. (Since your question asks about Python 2, this does not apply but it does going forward for Python 3)
Lastly, the unzipped folders will need to be added to the PYTHONPATH if they are not already on it.
I am trying to import some C code for use in my Golang application. I am really new to Go so I most likely have done something stupid here:
I have one file main.go in the main directory and yolo.go in the /yolo subdirectory along with all my C code.
yolo.go
package yolo
import "C"
func Vals() string {
return "Good to go!"
}
main.go
package main
import "fmt"
import "./yolo"
func main() {
fmt.Printf(yolo.Vals())
fmt.Printf("test")
}
This seems to compile fine - but none of the code in the main() function in main.go runs - no output is given, but no errors are either. If I forget about the import "C" everything runs just fine and I get the output I expect. What am I doing wrong?
Does Dart have some function or property that can tell whether the OS uses "/" or "\" in the file system paths, like File.separator in Java?
import 'dart:io';
Platform.pathSeparator
Small correction from previous answer.
import 'dart:io';
...
Platform.Platform.pathSeparator
See also Dart by example - Dart I/O and Command Line Apps - Getting the path separator for the current platform