Forum Archive

I cannot import modules

bobsmith76

First, very basic question. I literally cannot enter a change in the python text unless I swipe left. When I hit a button and the change registers the drop box will remain up and I cannot get it go down unless I swipe left. Surely, this was not the designers intention.

Second, I've got my python files on my phone but when I run my program and it tries to import another module I get an error. The error message is:

cannot import module __main__.dictionary_new.py

I have the dictionary_new.py open in the external files. I'm thinking that maybe my problem is that I cannot get the pycache folder into my phone. I touch the pycache folder which has a dictionary_new.pyc file in it but nothing happens when I click on it. Could that be the reason why the module is not being imported?

bobsmith76

Also the text editor removed the dunderscores. The file says main.dictionary_new.py, as listed above. dunderscores are quite important so I don't why they have been programmed to be deleted.

ccc

Try removing the .py at the end.

Put text in backticks (`my text`) if you do not want the editor to interprete it as markdown.

bobsmith76

I didn't have '.py' attached to begin with. Here is the exact syntax I'm using:

from openpyxl import load_workbook
from collections import Counter
import copy
import time
import operator
import sys
import cProfile
import re
from pprint import pprint
import collections
import os

from dictionary_new import large_dict
from claims_new import pop_sent
from start_and_stop import info

Everything works until I hit from dictionary_new and it says the module is not found.

That code clearly works on my laptop but it is not working on pythonista 3. I have the dictionary_new file in my external files, the same place as the file I'm using. Are you it doesn't have something to do with __pychache__?

Also I didn't get an answer to the very basic necessity of being able to input changes into the file and exiting the editor without having to swipe left.

zrzka

Don't have iPad around, but I assume that external files / bookmarks are not in sys.path. Try to add it to sys.path before importing. Works on desktop is irrelevant, IDE can add it, etc.

ccc

Try adding a leading dot as in from .dictionary_new import large_dict.

Is your laptop running Python 3 or legacy Python (python -V)?

zrzka

Don't use relative imports. They also don't work outside of package.

Pythonista sys.path contains system paths and site-packages, -2, -3. Current / script dir is not in sys.path.

If it works on desktop, IDE is probably adding it as I wrote. At least PyCharm does this for example.

What can you do?

a.py:

def a():
    print('A')

b.py in the same folder:

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))

import a
a.a()
bobsmith76

Putting a dot in front of the files doesn't work. I'm using Python 3.6. I don't know what you mean by sys.path. I also still don't have an answer my question about how do you register changes to the code other than swiping left. To be more clear when I touch the code and make a change, a pop up keyboard appears but I cannot make this pop keyboard go away unless I swipe left. Is that how it's supposed to be done? Because that seems rather strange to me.

bobsmith76

@zrzka Thanks for helping me out but I cannot figure out what you want me to do? I'm still quite a rookie programmer so if you could speak in a somewhat more dumbed -down language that would help out. Reading over your post I can't really figure out what you're recommending.

JonB

Can you take a step back, and describe what folders you have, and what files are in each folder? What file are you prsssing "play" on, and where is it located?

The way pythonisya works, any imports must either
1) live in the same folder as the file you pressed play on
2) live in the site-packages folder
or
3) live in a folder that can be found in sys.path

try this: before your import, insert the line:

import sys,os
print(sys.path)
print(os.path.abspath('.'))
print(os.listdir())

and paste the results back here, or post to a gist.

As an aside, I am not sure if .pyc files can be imported in pythonista. Try using the .py file instead.

cvp

@bobsmith76 pressing forces keyboard to disappear

JonB

@zrzka The current script gets added to sys.path, just before it is run. So, running file a can import b in the same path.

zrzka

@JonB yup, taking back my comment. Just tried on iPhone and it really works. I had to do smth wrong before.

P..S. Admire all people writing scripts on iPhone, it's a nightmare for me ;)

bobsmith76

@cvp thanks

bobsmith76

I tried running my other files and I'm getting [errno 1] operation not permitted. And I tried this with a very simply file which is nothing other than:

import sys,os
print (sys.path)
print (os.path.abspath('.'))
print (os.listdir())

def info():

order = [0, 0, 0, "#", 11, 14, 21, 71, 73, 79, 93, 178]
get_words_used = 0
proof_type = 4

I also tried running the print commands like your recommended on the main file but I get the same error message as before. Also, I tried moving the files into the folder SCRIPT LIBRARY - THIS IPOD. but that didn't help. at the very end of the error message i do get the line:

pythonista3/documents and then it lists all the files in that folder and all the files that I need to run my program are in that file.

bobsmith76

Here, I managed to copy the error message and email it to myself. This is what it says and this is for an even more basic code. All this code says is:

import sys,os
print (sys.path)
print (os.path.abspath('.'))
print (os.listdir())

print ('hey')

And as you'll see it does print 'hey' at the bottom. Also when it says

'Examples', 'Templates', 'Welcome.md', 'init .py', 'claims_new.py', 'dictionary_new.py', 'intermed_code.py', 'new_code.py', 'site-packages-2', 'site-packages-3', 'start_and_stop.py']

Those are all the files I need to run my program. So they're all in the right place.

On second thought, I couldn't print the error message because your algorithm flagged it as spam, so you can read it here:

http://www.dreamincode.net/forums/blog/2557/entry-5112-to-be-deleted/

JonB

bobsmith,

We can only help you if you follow instructions exactly!
Insert those lines, then run your script, and Copy the entire output, along with the entire traceback, and paste that back here. Don't summarize it, paste the entire result.

In short : if the script you are running, and the script you are importing are in the same folder, you should be able to import it. You should be using the .py file, not the .pyc, since .pyc files are not guaranteed to be compatible across versions.

You could also place the file you want to import under Modules & Libraries / site-packages (or, if it is a package, with an __init__, then the entire folder can be in site-packages)

bobsmith76

Also I just tried playing brickbreaker and it works on my ipod so my ipod is probably not the problem.

bobsmith76

I told you that I tried to paste the traceback but your algorithm marked it as spam. So I pasted the traceback on another website. Did you read the traceback that I posted on the other website? Also, I have the __init__ file in the same folder as my other files. It says that on the traceback that your algorithm blocked.

bobsmith76

Any idea what I should do now? I've done everything you told me so I don't understand why the help suddenly dried up?

zrzka

This forum is not 24/7 support. Sitting in a pub, drinking beer, ... ;)

bobsmith76

cool, what type of beer and where is the pub?

Matteo

@bobsmith76 Hi. @zrzka is right about 24/7, for one reason: nobody here is paid to help anyone. Also @omz (Pythonista developer) can't answer to everyone asks something here.

But if you try to immagine to be the person that could help you, which kind of question would you prefer?

This simple question is in my opinion the only one that could help you on posting a good question (it is valid also for me: a good question in this case is: a question that has high probability to be answered).

So, sorry if I ask you something: can you post here a full-work example of code that creates the issues you have? If you don't want to post your work for any reason (privacy, ...) you should post a full-work limited example that reproduces the big code you are trying to use.

You can use here a dropbox link (or any other kind of cloud service) in order to allow user to download your example (with folders, files, scripts, ...) and try it with his/her own device (maybe someone could have some problems about executing some arbitrary code in his/her Pythonista for security reasons, but a full-working example shared with other people makes very easy to start to understand what and where is the problem you have with your code).

Regards
Bye

zrzka

@bobsmith76 Europe, Czech Republic, Rokytnice v Orlickych horach. I don't know which beer it is, some local one. Testing what do they have.

@Matteo it wasn't about paid support, happy to help for free, but people tend to forget that we live in different time zones, one can have morning, another one evening, ...

Matteo

@zrzka yes, you are right. Howerver it is also good when people (like @bobsmith76) ask something because want to understand deeper, to learn something new and to solve problems. Sometimes I can't sleep when I have something I can't understand...
Anyway, @bobsmith76 try to post link to a full-work example that reproduces your problems.
Bye

bobsmith76

I'll wait for JonB to read the error message that I posted. Then we'll go from there.
http://www.dreamincode.net/forums/blog/2557/entry-5112-to-be-deleted/

dgelessus

@bobsmith76 That's an empty page (in Safari on my iPhone with iOS 10.3.3).

For uploading code or text snippets I'd suggest https://gist.github.com/ or https://pastebin.com/. Or you can wait a couple of days and try posing the text again, sometimes @omz manually whitelists legitimate users caught in the spam filter.

JonB

@bobsmith76 okay, now add to your simple script your failing import statement.

If you get an exception, copy the full traceback. you should be able to paste it to the forum with backticks, but if not, copy it into a new file in pythonista, and from the wrench menu, you can share it to an anonymous gist, then paste the link.. that is easier than emailing it.

bobsmith76

Here is my gist

https://gist.github.com/anonymous/20cd6b7383a4875b20bc01724c08fecd

And here is my traceback, at least I think this is what that is. Once again, thanks for helping me out. I still cannot paste it in this forum even when I use backticks so here is a link a pastebin.

https://pastebin.com/11QC4Zjd

bobsmith76

here is the traceback for the program that I really want to run

https://pastebin.com/kA5ZVzdu

Let me post the complete code and expected output shortly.

bobsmith76

expected output
https://pastebin.com/REKUk066

code itself

https://gist.github.com/kylefoley76/aef041adf0491be00a54a2f0dc070b2f
https://gist.github.com/kylefoley76/d40930595276eae49ee2d40c5063b2e0
https://gist.github.com/kylefoley76/56a12fbd4722a50072ba864128c0b8a5
https://gist.github.com/kylefoley76/e054d3fe7a5fcdf02b023c9dbc9228a0

JonB

Which is the script you are running from the Play button?
I see from dictionary_new import large_dict
yet the traceback shows from .dictionary_new

Here is what I did:
1) I created a folder in This Ipad (bobsmith)
2) I put your scripts in the folder
3) pressed play on intermed_code.py -- it imported dictionary_new without an issue.

If the issue is that you were testing things out in the console, or another script (import intermed_code), then you may need to set sys.path
sys.path.insert(0,os.path.expanduser('~/Documents/bobsmith')

bobsmith76

got it working. The problem was that there were four possibilities and I was only trying 3 of them.
1. dots before file name in the external library
2. no dots before file name in the external library
3. dots before file name in the script library
4. no dots before file name in the script library

The fourth possibility was the one that worked. I want to thank you for sticking with me through this trying ordeal.

bobsmith76

Also, I want to add that pythonista is a real big boost for me. You can't just whip out your laptop at a party and say 'hey, look I can calculate the consistency of sentences' but you can do that if you have an iphone.