Having a strange error here. I had a previous directory structured as follows
main.py
foldername
modulename.py
now in main.py I called modulename.py by using import modulename.py as mn
now I am making changes but I still want to use the modules as before, so in a completely different location on my computer I have set up the structure as before:
newmain.py
foldername
modulename.py
to make sure I am working in the right directory I used:
import os
os.chdir('NewMainPath/')
which correctly sets the directory of the file but if I now use
import modulename.py as mn
it for some reason is linking to the previous modulename.py
That means if I make any changes it won't pick them up and is just linking to the previous location and I can't figure out why.
Instead of using os.chdir(newpath)
Try using
import sys
sys.path.insert(0, '/newpath')
import file
Also worth reading: Importing files from different folder in Python
Related
I'm trying to import from a parent folder. The folder structure is (ignore the names)
experiments
__init__.py
poppy.py
ostepop
__ init__.py
importio.py
nopop
__ init__.py
loggio.py
I tried adding __init__.pyto every folder, but it didn't have any affect
import experiments.ostepop.loggiogives error message:
ModuleNotFoundError: No module named 'experiments'
and from ..experiments import poppy gives
ImportError: attempted relative import with no known parent package
Any tips on how to import poppy.py or loggio.py from importio.py?
import os
import sys
# Append parent directory to import path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
After this you can import your modules as if they were at, or under, the same directory as your source file. So from importio.py you can
import poppy # noqa
import nopop.logio # noqa
The # noqa is there to suppress "PEP 8: E402 module level import not at top of file" warning.
The __init__.py files are unnecessary for this solution, but you may want to keep them (except the top level one), if you plan to convert your subdirectories to modules one day.
If you want to understand how this solution work, please run
print(__file__)
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
It will print:
The current file name
The current file name, with absolute path
The current directory, with absolute path
And the parent directory, with absolute path
And sys.path.insert(0, ...) inserts this path to the beginning of the search path.
They are plenty of posts already, but it's still a tricky stuff to me as well.
So, though I'm still not sure what's the best practice, anyway here is how I handled this problem.
In the module where you want to do the imports, add
import sys
import os
sys.path.append(os.path.join(sys.path[0], '..'))
sys.path[0] is the absolute path to the module without the file name. It works both when the module is and is not the top-level script. If you don't know what is top-level script and how is it related to import mechanism, read this.
os.path.join is used to probably handle system difference. Anyway, os.path.join(sys.path[0], '..') will point to the parent path.
sys.path.append add that parent path to the searching space.
So, in your case, add previous code to importio.py, then you can do
from nopop import loggio
import poppy
in importio.py as well.
__init__.py is not used at all in this solution. You can delete them.
But I've seen many repos using __init__.py. So the best practice might involve using them.
If I understand you correctly, you want to import poppy.py from importio.py and debug with file model. Under this model, Python interpreter will not think of it as a package, so this code (import experiments.ostepop.loggio and from ..experiments import poppy) is invalid.
To solve this problem, you can debug this script with package model, which means run python -m experiments in terminal. It may be run correctly.
here is my Maya.env
λ cat C:\Users\roroco\Documents\maya\2018\Maya.env
MAYA_SCRIPT_PATH=C:/Users/roroco/OneDrive/maya/script
MAYA_PLUGIN_IN_PATH=C:/Users/roroco/OneDrive/maya/plugin
I'm sure my script exist:
λ cat C:\Users\roroco\OneDrive\maya\script\ro.py
def init():
print("prpr")
and in maya script editor, my script dir exist in MAYA_PATH_DIR
getenv MAYA_SCRIPT_PATH;
// Result: C:/Users/roroco/Documents/maya/projects/default/scripts;C:/Users/roroco/OneDrive/maya/script;C:/Users/roroco/Documents/maya/2018/scripts;C:/Users/roroco/Documents/maya/scripts;C:/Users/roroco/Documents/maya/2018/presets;C:/Users/roroco/Documents/maya/2018/prefs/shelves;C:/Users/roroco/Documents/maya/2018/prefs/markingMenus...
but when I import ro in script editor, it raise:
# Error: ImportError: file <maya console> line 1: No module named ro #
I hope I can write my maya script and auto sync to onedrive, how should i do
I don't understand maya doc say i can set MAYA_SCRIPT_PATH why it doesn't work
My tmp solution is add my script_dir in maya startup script
in C:\Users\roroco\Documents\maya\scripts\userSetup.py, add following code:
import imp
import os
imp.load_source("", "c:/users/" + os.environ["USERNAME"] + "/OneDrive/maya/script/userSetup.py")
and in my_script_dir/userSetup.py add my_script_dir to sys.path
import sys
import os
ro_script = os.path.dirname(__file__)
if ro_script not in sys.path:
sys.path.insert(0, ro_script)
you should be able to add python paths to your running maya dynamically with sys.path.insert or sys.path.append. You should not need to do anything more elaborate than that; you script should be importable if it's on the sys path.
For what you're trying to do a very common and clean solution is to use maya modules. Modules will allow you to append to the usual search directories, and they support network shares -- so you would add a module (in your maya modules path) and point its scripts directory to your onedrive. Modules can have their own userSetup.pys as well, which is very nice for keeping a clean separation between your code and anything else running on a particular machine.
More on modules here and here. The docs are here
You shouldn't have to add your path dynamically through userSetup.py.
Make sure you include PYTHONPATH in your Maya.env:
PYTHONPATH=C:\Users\roroco\OneDrive\maya\script
Checking my Windows home setup, I'm using back slashes in Maya.env, so you can see if switching helps. Instead of checking MAYA_PATH_DIR, check your sys.path to see if your directory is there. All Python modules need to be in one of these directories to import it:
import sys
for p in sys.path:
print p
You can also copy and paste your path to os.path.exists to confirm it really does exist. Sometimes it's the right path but the slashes make it so it doesn't resolve properly.
I am trying to import from relative path in my python program.
the class i would like to import is in
home/foo/bar/model.py
However, my current python script is in
home/best/user/test.py
i have tried to use
from ../../foo/bar import class
But it throws up a syntax error
When importing modules, python looks in the current working directory and in the paths in sys.path. You can add the directory of the script you would like to import to sys.path:
import sys
sys.path.append('home/foo/bar')
import model # imports home/foo/bar/model.py
You can't do that. You can't import from an explicitly specified path (without awful trickery). All Python imports are based on the systemwide import paths (in sys,path). You can't import anything that isn't reachable from sys,path (i.e., it's either on sys.path itself or it's inside a package that's on sys.path). The documenation has the details. If you want to be able to import from that file, you need to somehow add its directory (or the directory of its topmost containing package) to the path.
I created a test file called test.py (contained a class named test) and saved it into documents(Mac OS X 10.9.3)
I then attempted to use this file by writing from test import test. However, I got an error telling me that there was no module named test. Please can you shed some light into this issue.
The problem is that the Documents folder isn't usually in the PATH or PYTHONPATH, so the interpreter can't see scripts that are stored there (unless Documents happens to be the current working directory).
I can think of two solutions:
Move the file test.py to somewhere within Python's path.
You can find your PYTHONPATH with this script:
import os
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
(From Mark Ransom’s answer to How do I find out my python path using python?)
You can find the system PATH with
import sys
print sys.path
Both of these return a list of folders. If you put test.py in one of those folders, then it will be seen by the interpreter.
For example, I usually install custom modules at
/Library/Python/2.7/site-packages
and then I can import them as normal.
Manually append Documents to your path.
You can use sys.path.append to add a directory to the path.
If you only need to use test.py in a handful of scripts, then you can add this code to each script you want to use test.py in:
import os
import sys
sys.path.append(os.path.join(os.environ['HOME'], 'Documents'))
from test import test
Here is my file structure that I am working with for my application. My problem is that I cannot get my test_ctd.py file to see my ctd.py file.
Here is my directory structure
FileParser
--Parsers
----ctd.py
--tests
----__init__.py
----test_ctd.py
--parse.py
I never used an init.py file and am struggling to understand it, but here is my attempt at adding ctd.py to my path.
import sys
import os.path
d = os.path.dirname(os.path.dirname(os.path.abspath('../../')))
from Parsers import ctd
Also I do not have any code in my parse.py file, but I will be using that to initiate the program. Would I need a init file for that as well so I can import the files from the Parsers folder?
Any help on how to access my files from within this program structure would be appreciated. Eventually it will be running on a web server, not sure if that makes a difference or not...
Thanks!
Parsers and FileParser must contain __init__.py if you want to import something from ctd.py. See Importing modules in Python and __init__.py.
Then, you can import ctd.py from your tests scripts by doing relative imports like from ..Parsers import ctd or by adding FileParser to sys.path and using from Parsers import ctd.
Or, add the directory containing FileParser to sys.path and use from FileParser.Parsers import ctd.
Hope that helps.
You need to make sure Python is actually looking in the right places. You can do this by modifying your PYTHONPATH environment variable to include places where Python packages are found (such as this directory). You'll also need an __init__.py file, to mark the directory as a Python package.
Or, the cheap, hacky way is by modifying sys.path.
import sys
import os
sys.path.insert(0, os.path.join(os.path.abspath(os.path.dirname(__file__)), 'Parsers'))
import cdt
Move the __init__.py file into Parsers and add the directory FileParser as absolute path to your PYTHONPATH. For example with sys.path.append('full/path/to/FileParser').