I am working in:
Eclipse
Windows 7
64-bit Python 3.3
I want to import writer.pyx (yes, Cython) into main.py. At the top of main.py, I have the appropriate import statement:
import writer
Both main.py and writer.pyx are in the same directory, and that directory is also in Windows' PYTHONPATH environment variable. However, it gives me the error ImportError: No module named 'writer'. So, as far as I can tell, it should be working.
But, here's the kicker: in that same directory, there's a file called reader.pyx that I'm also importing in main.py - and it works perfectly. No issues, no errors.
So, clear summary:
main.py is importing writer.pyx and reader.pyx
All three files are in the same directory (and PYTHONPATH lists that directory)
reader.pyx imports fine, but writer.pyx throws an ImportError: No module named 'writer'
Any ideas as to how I can fix this?
Visual representation:
import reader
import writer
def function():
# code
P.S. This is not my code, and it used to run just fine on this very computer, and the code has not been changed since. This leads me to believe it's an environment problem, but I'm not sure what. Something with Cython, perhaps? I don't have any real experience with it.
From what I understand, pyx files need to be compiled before they can be loaded. You can do this from within your script by using pyximport, if you first issue:
import pyximport; pyximport.install(pyimport = True)
On top of that, based on the fact that there seems to be another reader.py in your path, I'd suggest you create a folder in the same directory where main.py resides (say you name it test_imports) and put both reader.pyx and writer.pyx there, so that you're sure you're importing those files when you issue:
from test_imports import reader, writer
Note that the test_imports directory will also need an empty __init__.py file that tells Python it is a package.
Are you working in a package? If so you will need to import it using the package name:
from mypackage import writer
There are two option I see.
First, remember that your PYTHONPATH may be different in you eclipse and in your windows. Eclipses changes the path.
Second, please try this
from writer import *
and let us know if the writer package will be found
Related
I am trying to understand how import works in jupyter notebook.
My present working directory is "home/username". I have three python modules.
The path names of these modules are as given below.
"/home/username/module1.py"
"/home/username/folder/module2.py"
"/home/username/anaconda3/lib/python3.7/os.py" (which is an inbuilt python module)
Jupyter Notebook:
cell 1:
import module1
Works just fine
cell 2:
import module2 gives
ModuleNotFoundError: No module named 'module2'
cell 3:
import os
Works just fine
It seems like modules in the working directory can be imported without any problem. So, module1.py can be imported. Modules in other directories that are not packages cannot be imported directly. So, module2.py throws an error. But if this is the case how can os.py, which is not the working directory or in another package in the same directory, be imported directly?
This is really more about how python itself works.
You should be able to import module2 with from folder import module2. You should declare /home/username/folder as a package by create a blank init file /home/username/folder/__init__py. I recommend naming the package something more unique, like potrus_folder, that way you don't get naming conflicts down the line.
To explain: Python keeps track of what modules it has available through it's path, it is usually set in your environment variables. To see what folders it looks in for modules you can do import sys then print(sys.path). By default your working directory (/home/username/) will be included, with highest priority (it should thus be either first or last in sys.path, I don't remember). You can add your own folder with sys.path.append('/some/folder'), although it is frowned upon, and you should really add it to your system path, or just keep it as a package in your working directory.
Packages are really just subfolders of paths which have already been added. You access them, as I explained earlier, by using the from X import Y syntax, or if you want to go deeper from X.Z import Y. Remember the __init__.py file.
The path of os library is set in environment*
Whenever you give import it would search all the directories which are added in your environment + the pwd , so you could just add the directory in environment and that would work
By default /home/username/anaconda3/lib/python3.7/ is added by default at the time of installation since there is where most of the module lies, but you can add urs too
I'm following a tutorial to call python code from a C++ program from the python docs.
Everything works just fine when trying to call the multiply example. Now if I add a line to the python source code importing a library, lets say openpyxl,
from openpyxl import load_workbook
I receive an error from python
ImportError: No module named openpyxl
I thought if I import a system library, I wouldn't have any problems, but I also get an error if I try to import datetime.
I don't have any error if I import the file from the python console. The openpyxl library is installed in my system.
So my question is: how to import python source code that needs to import packages?
EDIT: Ok, I forgot to mention something, I have not been completely honest with you guys, I'm sorry.
Trying to run the example I run into a problem: I couldn't make python found my multiply.py file, and the line PyImport_Import always return null.
My solution was to add the path in which I knew my python source was by using PySys_SetPath. The problem is that I just realized that this function doesn't append a new directory, it just overwrites the PYTHONPATH. So now python can find multiply.py, but absolutly anything else.
Of course I've deleted that line but now I have another question, why does python can't find my source if the file is just in the same directory of the C++ compiled program?
The I realized that my sys.path from my python console was a little different from the path showed in my embedded python: the first one had at the beginning of the list an empty string ''. I'm not a python expert, but when I add that line to my path I could import the multiply.py so it seems that was the reason I couldn't import modules that were located to relative to my executable was the missing of this empty path -but still don't know what it means-.
I have to thank to #paul-evans who give me the idea of adding the path to find my files.
This is what PYTHONPATH is for. You can set it as an environment variable containing a list module directories, or in the code itself something like:
import sys
sys.path.append("path/to/openpyxl/module")
I am experimenting with python, mostly troubleshooting other people's code. I am trying to get a program to run, "path\folderA\program.py".
I am running the program from path\folderA
I am getting an error:
ImportError: No module named fff.ggg.ppp
program.py contains an import:
from fff.ggg.ppp import mmm
In the folder "path\folderB" there are:
"path\folderB\fff\__init__.py"
"path\folderB\fff\ggg"
folder ggg also contains __init__.py, as well as program ppp.py
From reading other posts, like Python error "ImportError: No module named" I understand that having the __init__.py makes a folder a "package" which makes imports from it possible - but it doesn't work, since I am getting an error.
This has been working for other people that worked with these projects, so there is something wrong with my setup.
I read something about the directories having to be in the sys.path. Does that mean I have to add them to the environment variable path ? That would mean adding a lot of directories to the PATH though, so it can't be.
So I also found the following:
import sys
sys.path.append( <path to FolderB> )
But that means changing the code (which has not been necessary for other people) and hard-coding a path to what it is on my local machine - which I shouldn't have to, right ?
I can't visualize it - apparently I am not supposed to change the code and hard-code the physical path to the import module - so how can a program from folderA even know to look in folderB for an import ?
How does the magic of __init__.py work ?
I can't visualize it - apparently I am not supposed to change the code
and hard-code the physical path to the import module - so how can a
program from folderA even know to look in folderB for an import ?
You are correct. Somehow you have to tell python to look for imported modules in folderB. There is no __init__.py magic that lets you import from other folders on your hard drive.
Usually, if you've got various different python packages like that, they work by being installed into python's library. That way they can imported from anywhere. This is usually accomplished by a setup.py script. Check if folderB has one. Run it with python setup.py install.
If that doesn't work, we'll need more information about how this code is structured.
Folder B must be on the sys.path, so you would either need to move mmm to A, or modify sys.path from within A (not sure if that works). __init__.py tells python that the folder is a package, so you could have folders with __init__.py within folders with __init__.py and python treats the folders inside as parts of the parent folder. Check out sympy or almost any large python library and you will find such a structure. It can also contain code to be run on import, but can also be empty.
My situation is similar to one in this question... The difference is,
In our python/django project, we have a directory called utils, which keeps basic functions...
Sometimes, we need to test some modules by running thm from console like
python myproject/some_module.py
It is all good, until python tries to import something from our utils directory...
from utils.custom_modules import some_function
ImportError: No module named custom_modules
I check my python path, and our project is on the list, each folder under the project file has __init__.py files, and when i run ipython within project directory... Everything is ok, otherwise, python imports from its own utils directory...
My collegues use the sama method without any problem, but it throws ImportError on my environment... What could be the problem that all of us was missing?
UPDATE: My project directory, and each sub-drectory have __init__.py file, and i can import other modules from my project without any problem... When i am in a diffrent folder than my procekt and i run ipython, a such import have no problem...
from someothermodule.submodule imprort blahblahblah
But, when it comes to importing utils, it fails...
UPATE 2: What caused the problem was the utils directory under django folder, which is also in the python path...
See the PEP on absolute and relative imports for the semantics. You probably want
from .utils.custom_modules import some_function
if you're in a file at the top level of your package.
Edit: This can only be done from inside a package. This is for a good reason -- if you're importing something that is part of your project, then you're already treating it like a Python package, and you should actually make it one. You do this by adding an __init__.py file to project directory.
Edit 2: You've completely changed the question. It may be possible to work around the problem, but the correct thing to do is not refer to your packed the same way as a builtin package. You either need to rename utils, or make it a subpackage of another package, so you can refer to it by a non-conflicting name (like from mydjangoapp.utils.custom_modules import some_function).
I'm not going to bother telling you to try and make sure you don't name your own modules after the stdlib modules;
If you want to leave the name like this, you'll have to use something like this in everything that imports your own utils module:
import sys, imp
utils = sys.modules.get('utils')
if not utils: utils = imp.load_module('utils',*imp.find_module('utils/utils'))
However, unless there's a lot of stuff that you'd have to change after renaming it, I'd suggest you rename it.
I'm really new to Python. I'm trying to import a third party module called primes.py. I have placed this module in C:\Python26\Lib (the location where I installed Python). I then have another file which is trying to import this module. The file attempting to import primes is located at C:\Python26.
In my Python file I have the following two lines:
import primes
import sys
When I run this file, I get the following error:
ImportError: No module named primes
Can anyone help me out?
The module needs to be on your PYTHONPATH or in the same directory as the script, app, or module that is trying to import the module.
I'm not a Windows programmer but if you have placed the module in 'C:\Python26\Lib' and your path is set to 'C:\Python26' you need to add '\Python26\Lib' to your PYTHONPATH. I'm not certain on what the syntax would be but it should be something like 'C:\Python26;C:\Python26\Lib'. Assuming everything is the same on Windows, the subdirectories are not searched automatically.
I think a more appropriate place to put the module is to place it in 'site-packages', I don't know how this is accomplished on Windows. On *nix systems there is a script 'setup.py' that comes with the package/module, and uses 'setuptools' to build and install the package/module for you.
you probably should located this under site-packages directory or a private folder instead. Check your sys.path to understand your import paths.
Put primes.py in the lib/site-packages/ directory.
Also: no need to put your own Python files under the installation directory: I'd advise you to put them somewhere else (where it makes sense).