I want to import a python package foo into a new project in Aptana.
I tried two methods: 1. promote file folder to project and 2. import folder into a new project.
In both cases, the error: "no module named foo.filename" occurs when I try to run
a program in a subfolder. The parent folder is called foo. The code is:
from foo.filename import *
There are no reference errors when executing this file outside Aptana.
How can I fix this?
Make sure each folder in your package contains an:
__init__.py file.
If the package folder is not inside of the projects root source directory you may want to add it to the PYTHONPATH environment variable.
Related
I am working on some python project (2.7) and I have issue with imports. When I run main.py it start scripts from tests folder etc. and save output to logs and everything works fine.
/root------------
-logs
-staticCfg
-config.py
-tests
-systemTest
-scrypt1.py
-scrypt2.py
-userTest
-uScrypt1.py
main.py
My static variables (email, name etc.) are located in config.py. I need to import config.py in scrypt1.py or scrypt2.py. I tryed adding __init__.py to tests, systemTest and staticCfg folder but I always get an error.
In my scrypt1.py:
import staticCfg as cfg
...
or
from staticCfg import *
...
I get the error:
ImportError: No module named staticCfg
The import mechanism of Python can be a bit tricky.
You can refer to the documentation for more information: Python Import Mechanism
When you use absolute imports (your import does not start with a .) as you do, the import path will start from your main script (the one you launch). In your case, it's scrypt1.py. So starting from this location, python can't find the package staticCfg.
For me, the simplest solution is to create a main script in your root directory and call scrypt1.py from there (imported using from tests.systemTet import scrypt1.py). In this case, the base package will be your root folder and you will have access to the package staticCfg from all your script files as you wanted to do.
you may add root folder to PYTHONPATH.
My folder structure in pycharm is as follows.
--python
--concepts
--common
--myds.py
--__init__.py
--data_structures
--test_ds.py
I have the following line in test_ds.py
from common import my_ds
I get the following error.
ImportError: No module named 'common'
I have added common to Settings --> Project Interpreter -> Interpreter Paths
and the folder shows up as library root.
Still why am I getting this error.
Try from ..common import my_ds. Also make sure that it has an __init__.py file in that directory (not required but it's good practice).
As for the .. they indicate that you're importing from the parent package to the one you're currently on.
You need to make your common folder into a python package in order to import it in python. I think you've tried to do it and created init file in your common folder but actually it must be __init__.py. Rename it like this and then your package will be visible to python.
Hope it helps!
I have a custom module that I am trying to read from a folder under a hierarchy:
> project-source
/tests
/provider
my_provider.py
settings_mock.py
__init__.py
I am trying to call, from my_provider.py
import tests.settings_mock as settings
Example from command line:
project-source> python tests/provider/my_provider.py
Error:
... ImportError: No module named settings_mock
I keep getting No module named settings_mock as error. I have already exported project_source path to PYTHONPATH. I have made tests into a package by creating a __init__.py file in its root, but no change in the error then.
I can print the settings_mock.py attributes when cd'ing project source
>>> import tests.settings_mock as settings
>>> print settings.storage_provider
correct storage provider value
Is anyone able to point out my mistake here? Thanks!
You only have one small mistake. To use subfolders, you need __init__.py, not init.py as you stated in the question. The difference is that __init__ is a builtin function of python, whereas init is not. Having this file in each subfolder tells the pyhon interpreter that the folder is a "package" that needs to be initialized.
UPDATED: It should be noted that python usually runs from the current directory that the script is located. If your executable main script is my_provider.py, then it's not going to know what to import, since the main script is located in a lower directory than the object it is trying to import. Think of it as a hierarchy. Scripts can only import things that are beneath them. Try separating out the executable from everything else in that file, if there are things that settings_mock needs to import.
I'm totally new to Python, and I want to create my first Python library for peronal uses.
I'm using Python 2.7.5, and running the IDLE interface.
So far, what I understood from the documentation and related questions is that:
Python goes through a list of directories listed in sys.path to find scripts and libraries
The package directory must contain a __init__.py file, which can be empty
The module I want to create should be a modulename.py file with the code inside the package directory
(Sources: http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html --- https://docs.python.org/2/tutorial/modules.html)
And here is what I tried that fails:
Created a personal package directory C:\....\pythonlibs
Created a subpackage dir C:\....\pythonlibs\package
Created the __init__.py file inside both folders
Created the mymodule.py file in the packacge dir
And then in the IDLE used this code:
import sys
sys.path.append(r'C:\....\pythonlibs')
First issue:
Currently I have to do this append every time I enter the IDLE. How can I keep the directory in sys.path permanently just as there are a lot of other directories there?
Then I tried importing my package:
import pythonlibs #fails!! why?
import pythonlibs.package #fails!! why?
import package #works
The error is: ImportError: No module named pythonlibs
Second issue?
This seems to be against the documentation, why can't I import from the root pythonlibs folder?
With line
sys.path.append(r'C:\....\pythonlibs')
you are instructing interpreter to start looking for modules (libraries) in this directory. Since this directory does not contain pythonlibs folder (the parent does), it can't import it.
Similarly - because it contains the module package, it can import it.
I am trying to complete Exercise 47 of Learn Python The Hard Way, 2nd Edition, but I am receiving this error when I run tests/ex47_tests.py:
File "tests/ex47_tests.py", line 3, in <module>
from ex47.game import Room
ImportError: No module named ex47.game
I thought it was something I was doing wrong within my code because I am very new at this, so I cloned this repo from a GitHub user who seems to have successfully completed the exercise.
Not only are the relevant parts of our code identical, but I receive the same error when I try to run the tests/ex47_tests.py that I cloned from him. So now I am lost and hoping that someone has a solution for me. Any ideas?
fabrizioM's answer should get it to work. Here is a little explanation.
When Python loads a file, it searches on the filesystem. So here, we have the import statement:
from ex47.game import Room
It looks for the file ex47.py on the modules search path (accessible as sys.path in Python code). The modules search path contains some directories based on the installation details of Python, the directories listed in the PYTHONPATH environment variable, and contains the parent directory of the script you’re executing. It doesn't find ex47.py on the path, but it sees there is a directory named ex47 with __init__.py inside of it. It then finds game.py in that folder.
The problem is that your current folder is not on the modules search path. Because ex47_tests.py was run, it has $cwd/tests on the path. You need $cwd on the path.
PYTHONPATH=. python tests/ex47_tests.py
does exactly that. It puts the $cwd on the modules search path so Python can find the source files.
You can also do:
python -m tests.ex47_tests
This will run it as a module instead of a file, while it will use the current directory as path it adds automatically to the the modules search path instead of the directory the file is located inside.
from the repository directory :
PYTHONPATH=. python tests/ex47_tests.py
Make sure there are no other ex47.py files/packages in your path.
The book asks you to copy the 'skeleton' directory and then use it for the game room exercise (#47). The skeleton directory has a "NAME" directory.
So if you copied the skeleton directory and named it ex47, you'll have another ex47 directory inside that.
lpthw > ex47 > ls
bin docs ex47 setup.py tests
So when the book says "Next, create a simple file ex47/game.py where you can put the code to test", you assume that it is the top level ex47. Not correct! That's why the import won't resolve.
I hit the same problem too until I realized that the book calls the outer directory (the one we got by copying 'skeleton') as 'simplegame' evident from this line in the book -
~/projects/simplegame $ nosetests
So all the answers about PYTHONPATH are valid, I just wanted to explain why the import wouldn't work for you!
There is something wrong with your directory structure. Here is my directory structure:
bin docs ex47 setup.py tests
./bin:
./docs:
./ex47:
game.py game.pyc __init__.py __init__.pyc
./tests:
ex47_tests.py ex47_tests.pyc __init__.py __init__.pyc
The project is named ex47 and there is a folder named ex47 within it.
(provided you have named your files according to his plan laid out before game.py file's code in this exercise)
When the author says create a simple file ex47/game.py he means the folder within is where the game.py file should be created and stored.
I too had the same error; being new to python, I followed the book example and after copying the skeleton directory over, renamed any files containing 'NAME' - one existed in the test/ directory and once removed the unable to load error disappeared - check the answer/contents in Microivan directory post above.
So within the project directory only two python files should exist, game.py and ex47_tests.py - hope you got there, on to the next exercise.!!