I've got the following file structure:
I'm trying to initialize some objects in main.py that belong to modules in the Listener, Parser and Configurations folders.
I understand I can't just write import listener since it's not in the same path.
What simple ways are there for the imports to work without adding the paths to the PYTHONPATH env variable?
Is there a way to make it work on any machine "out of the box" without the need to add the paths to PYTHONPATH or any solution like that? Preferably something with a relative path like in C++?
Possible duplicate: Importing from a relative path in Python
In short, you need to programmatically define PYTHONPATH in main.py, something like:
import sys, os
sys.path.append(os.path.dirname(__file__))
This implicitly adds current directory to PYTHONPATH. Rest of the part is straightforward
Create __init__.py file in each of the directories. After that, each of the modules can be imported as from Listener import ... or from Misc import ... etc.
The approach works 'out of box' without redefining any environmental variable.
Related
Working on Windows.
I need to import custom Python module custom_module. This custom module imports others custom modules.
The importing works fine when I open a terminal, launch a .bat script where I set the systems PATH and PYTHONPATH variables...
set PATH=C:/path/to/custmod2;C:/path/to/custmod3;C:/path/to/custmod4;
set PYTHONPATH=C:/path/to/custom_module
...then launch such Python script:
import custom_module
print("It works")
But I would like to set the PATH and PYTHONPATH system variables inside the Python's script.
However, when I modify the script in such manner:
import os
import sys
os.environ["PATH"] = "C:/path/to/custmod2;C:/path/to/custmod3;C:/path/to/custmod4;"
sys.path.append("C:/path/to/custom_module")
import custom_module
print("It works")
I've got error like ModuleNotFoundError: No module named 'some_module'.
As I understand, the sys.path.append() works fine, but setting os.environ["PATH"] does not give desired result. The some_module is literally some module that is imported either by the custom_module or some others module that is in using.
I've played with slashes and backslashes in the paths, put the modifier r before string beginning, and also tried different cases for the PATH key (like path, Path).
Thus, I have two questions:
Why os.environ["PATH"] does not work (however when to print() it, it contains what I've set)
How is it possible to update the system paths in Python script (or in some other nice approach) to be able to work with custom modules?
Thanks in advance!
I have an input error in pycharm when debugging and running.
My project structure is rooted properly, etc./HW3/. so that HW3 is the root directory.
I have a subfolder in HW3, util, and a file, util/util.py. I have another file in util called run_tests.py.
In run_tests.py, I have the following import structure,
from util.util import my_functions, etc.
This yields an input error, from util.util import load_dataset,proportionate_sample
ImportError: No module named 'util.util'; 'util' is not a package
However, in the exact same project, in another directory (same level as util) called data, I have a file data/data_prep.py, which also imports functions from util/util.py using a similar import statement...and it runs without any problems.
Obviously, I am doing this in the course of doing a homework, so please understand: this is ancillary to the scope of the homework.
The problem goes away when I move the file to another directory. So I guess this question is How do I import a python file located in the same directory in a pycharm project? Because pycharm raises an error if I just do import util and prompts me to use the full name from the root.
Recommended Way:
Make sure to set the working folder as Sources.
You can do it in Pycharm -> Preferences -> Project: XYZ -> Project Structure
Select your working folder and mark it as Sources. Then Pycharm recognize the working folder as a Source folder for the project and you will be able to simply add other files within that folder by using
import filename.py
or
from filename.py import mudule1
=================
Not recommended way:
In Pycharmyou can simply add . before the .py file which you are going to import it from the same folder. In your case it will be
from .util import my_functions
Resource
There is a good reference also for more information with example how to implement Package Relative Imports. I would highly recommend to check this page.
Package Relative Imports
If you don't have an __init__.py create one and add this line
from util.util import my_function
then you can easily import the module in your scripts
the __init__.py tells python that it should treat that folder as a python package, it can also be used to import/load modules too.
in most cases the __init__.py is empty.
Quoting the docs:
The __init__.py files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as string, from unintentionally
hiding valid modules that occur later on the module search path. In
the simplest case, __init__.py can just be an empty file, but it can
also execute initialization code for the package or set the __all__
variable, described later.
Right-click on the folder which you want to be marked as the source > Mark Directory as > Source root.
In my case, it worked only when I omit the extension. Example:
import filename
Note: May be a bit unrelated.
I was facing the same issue but I was unable to import a module in the same directory (rather than subdirectory as asked by OP) when running a jupyter notebook (here the directory didn't have __init__.py). Strangely, I had setup python path and interpreter location and everything. None of the other answers helped but changing the directory in python did.
import os
os.chdir(/path/to/your/directory/)
I'm using PyCharm 2017.3 on Ubuntu 16.04
I had the same issue with pycharm, but the actual mistake was that the file I was trying to import didn't have a .py extension, even though I was able to run it as a standalone script. Look in the explorer window and make sure it has a .py extension. If not, right click on the file in the explorer window, pick refactor, and then rename it with a .py extension.
In Pycharm go to "Run - Configuration" and uncheck
'Add Content root to Pythonpath' and
'Add source roots to Pythonpath',
then use
from filename import functionname
For me the issue was, the source directory was marked correctly, but my file to import was named starting with numeric value. Resolved by renaming it.
import 01_MyModuleToImport
to
import MyModuleToImport
I'm trying to run a python file, but I keep getting a ImportError.
My set up is I have a project with the following path:
/Users/John/Documents/pythonprojects/projectX
within 'projectX' I have a folder called 'components' which contains two python files titled 'py_file' and 'init'.
/Users/John/Documents/pythonprojects/projectX/components
At the top of 'py_file', I'm importing a namedTuple that is defined in 'init'
`from components import some_tuple`
When I run python py_file.py run I get ImportError: No module named components
However, if I add the lines below, I can get the file to run:
import sys
sys.path.append("..")
Any idea whats going on with this?
You should probably make sure your PYTHONPATH environment variable is set to /Users/John/Documents/pythonprojects/projectX.
Effectively, that is what you do with that line sys.path.append(".."), since the relative .. directory from py_file.py is /Users/John/Documents/pythonprojects/projectX.
from components import some_tuple
Python is looking for components in the directory you started it from. But, you started Python in your components directory, so that's not going to work. Appending ".." to your path appears to fix the problem because now Python will look in the parent directory, which does contain components.
However, the better approach is to leave sys.path alone. Figure out which .py file in your program is going to be the entry point, and make all your imports work when it executes from there. Then, always execute your program from there. If you want to quickly test some small module off in a package somewhere, write some tests!
If you'd like the flexibility of beginning execution from anywhere, you can look at relative imports. Personally I think these are better left until after absolute imports make sense.
I wanted to know if you can use the import statement in python from a directory that is not your local directory if that directory is not a package? Also, do all the directories on your system path have to be packages? If you add a relative path to your system path, what is it relative to?
you can alter sys.path in order to achieve all the results you are asking for.
Yes you can. To add a directory that is not your local directory:
import sys
sys.path += '/your_path/your_subpath/' # absolute paths
import your_package
If you need to load the module from an arbitrary path in the filesystem without adding it to sys.path you can use also imp.load_module
do all the directories on your system path have to be packages? No, they do not
If you add a relative path to your system path, what is it relative to?
to the directory containing the script that was used to invoke the Python interpreter.
I suggest, however, to set it in this way:
import sys,os
sys.path.append(os.path.realpath('..'))
or from the path of the script:
import sys,os
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(sys.argv[0]), '..')))
both the examples work also from interactive shells. Both examples ensure the relative path is what you meant, regardless of the OS
see also this post for more details on relative paths in python
That's really 3 different questions:
I wanted to know if you can use the import statement in python from a directory that is not your local directory if that directory is not a package
Yes, you can.
Also, do all the directories on your system path have to be packages?
No, they don't.
If you add a relative path to your system path, what is it relative to?
Relative to the current working directory.
I have a third-party Python script (foo.py) in a folder that is in my system path (but not the Python sys.path). foo.py is not part of any Python module.
I am writing another script (bar.py) in which I'd like to call a function located in foo.py. Is this possible? Can it be done without explicitly naming the folder of foo.py?
Thanks.
You can include the path of foo.py in the PYTHONPATH environment variable. The interpreter will look also the directories contained there, so you can make the import just like it was on the same directory.
If Python does not find the module, I don't think there's another way then to specify where it can be found, with one easy way being:
import sys
sys.path.append('/myfolder/itsinthisfolder/')
import foo