So i started reading the book called Learning Python. From the start there are many examples that you kinda do along as you read. And from the start i ran into a problem. First cmd wouldn't start python. So i googled and did the Path thing and it worked. Now python starts, but if i add anything to it, it doesn't. For example Python HelloWorld.py. It says it can't locate it. If i just drag and drop the file itself in the cmd window right after typing python it works. And that kindof fixes the problem. But there is a new problem when i try to import things.
% python >>> import myfile >>> print(myfile.title) The Meaning of Life
This is the example given. Inside myfile.py is just one like Title = 'The Meaning of Life'
So when i try to type in import myfile it sais "No module name 'mylife'. So the question is, where does python look for the module, and how do i change the directory he is looking at.
windows 8. My windows and python are on SSD, while i wanna keep all the practice things on my HDD.
Windows is a little bit gay it probably wants a more qualified location from you when you are rubbing things in cmd. Either absolute path or a proper relative path(./dongs.py).
For importing modules that aren't installed via pip, again it probably needs a more qualified location. For this, you can set your working directory and then import, and it should work? Use the os module and chdir method.
os.chdir('C://path/to/module/dir') import dongs
There is also __init__.py which should help I think.
idk I haven't used python on Windows in a while. It's fine, just slightly less intuitive than a Linux environment
Aaron Johnson
Not exactly the fix im looking for. Any other suggestions?
Nicholas Brown
On windows you should probably use dreampie
Dominic Evans
so this is not the way its done?
Benjamin Garcia
>dreampie Well Im starting from the very bottom. And trying to follow the book. Just to get the general idea of wtf im doing at least.
Jaxson Cooper
Development is much easier and intuitive on a Unix based OS like Ubuntu or OSX. Windows is geared towards "casual" users like grandparents, not for developers.
James Price
I'm sure there will come the time that i will get ubuntu, but for now, for the basics i think windows should work. I mean it should cause so much trouble from the very beginning
Aaron Scott
try this:
import os os.chdir('C:\Users\Aleksandr\Programming') import MyLife
Nah, you would need to: import os os.chdir('full_directory_path_of_your_module/your_file.py') import your_file
By the way, I've been coding in Python for 3 years and have never done this. I'm also 100% sure that it's bad practice.
If you write code and want to turn it into a shared module (e.g. code that needs to be accessed by multiple projects in multiple folders), then you should install it where pip installs all its modules. (depends on your OS)
Considering you're a beginner, I think you're getting off on the wrong foot. You shouldn't be worrying about this kind of stuff yet IMO. Stick more to the basics before learning about __init__.py and module creation type stuff
Cameron Peterson
Also don't include the "your_file.py" in the os.chdir(), I just included that to illustrate how the filename will be the same as your import name. But again, you really don't have to worry about this stuff right now IMO.
Grayson Collins
...
Ayden Rivera
Fuck why don't they explain this shit in the book. I've been stuck on this for the whole day lol
Owen Gonzalez
>for the basics i think windows should work. it does work, but windows is quirky and not linux, which is your most likely production environment for any project that other people will interact with
Owen Hill
See if this works: import os here = os.path.join(os.path.expanduser('~'), 'Programming') os.chdir(here)
Grayson Ward
type it out yourself and use tab auto complete. also see
Jaxson Davis
Thats the thing, its not supposed to be something i worry about. In the book it's like chapter 2. All it says is "Create MyFile.py, put title = text" then go to CMD and type python import myfile
Kevin White
Sup Forums is being a pile of shit and doesn't want to let me post.
Anyways, make a new folder on your desktop, put both python files in it and run the command prompt from that folder.
Robert Davis
Before opening python, navigate to the directory which holds the python file ('C:\Users\Aleksandr\Programming') using good old cd within CMD.
THEN, open python and import your file. This will save you from having to os.chdir() within python itself.
Mason Rodriguez
>If i just drag and drop the file itself in the cmd window right after typing python it works. you can do that?
Lucas Price
lol i was shocked too
Oliver Lee
I'm pretty sure even XP and Linux distros from 10 years ago let you do this. Also don't use cmd if you can, there are a billion alternatives.
Landon Evans
So python is way the fuck out there C:\Users\Aleksandr\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.6
While all the shit i need to start is here
C:\Users\Aleksandr\Programming
I assume the problem is that they are not in the same place. But when i start the cmd in the folder where all the simple programs are I can start the helloworld.py with python, but i can't import the myfile.py
Connor Bennett
>39 KB I like this. How'd you go about making it?
Jaxon Edwards
yeah the problem is the working directory for the python process. normally, in a python file, you can import other python files easily since the script you are running is usually in the same directory or in a parent directory
for example if i have dongs.py in ~/Documents/project, and fresh_dongs.py in ~/Documents/project, then import fresh_dongs should work when I run dongs.py
if fresh_dongs.py is in a subdir, like ~/Documents/project/pocket then my import statement in ~/Documents/project/dongs.py would look like import pocket.fresh_dongs
Bentley Jenkins
i figured that cmd is the shitties options, but im going along the book, to learn the basics.
Gavin Robinson
I may have been confusing before, but you don't include the ".py" with the import. So it'll be
Reduced framerate, quantization, GIF transparency and some other things. You can probably do better with a screen recorder and a bunch of ffmpeg shit, but it's good enough and I'm lazy.
David Adams
>I assume the problem is that they are not in the same place. But when i start the cmd in the folder where all the simple programs are I can start the helloworld.py with python, but i can't import the myfile.py
Yep, that is the problem.
Oliver Bennett
Thats the thing, the files themselves are in the same folder, just python is somewhere else. I assume that import looks for my module in the python directory. Yes/no?
Josiah Williams
but i should be able to just type out the place where the file i want to import is and it should work?
Xavier Jones
My god, Windows is such a pain in the ass.
Elijah Ross
...
Bentley Morris
oh boi
Austin Jenkins
Try capitalizing it maybe? import MyLife
And disable "hide extensions for known file types" in Explorer's settings, otherwise you end up with that .py.py.py shit and not being able to tell what anything is.
Tyler King
AFAIK Python does not use support full PATH for the imports. It does support relative importing (using ../../), but again this is all irrelevant for your case because in the real word, the file would be installed as a global module and imported with simply its name and no PATH needed.
I don't know how to fix your problem because I don't know how the python PATH was setup for Windows. On something like Ubuntu you literally don't have to think about this. It just works
Angel Lewis
oh my god... Why the fuck would capital letters matter. What is this shit? A whole day i've been googling for this?
Anyway, thanks to everyone who tried to help. Sup Forums for the rescue as always.
Colton Watson
Capitalizing won't do anything useful in his case. The filename is all lowercase, so the import will be as well.
Xavier Myers
Thanks Obama
Jeremiah Stewart
Oh shit, nevermind, I didn't see that his filename *was* capitalized.
Yeah, it's case sensitive. It's standard to keep filenames all lowercase as well.
Julian Rivera
The file in the book is lower case. But if you look at my file 2 letter are capitalized. Which has fixed the whole problem.
Connor Myers
From now on i will... This was a painfull lesson lol
Samuel Flores
>What is this shit? A whole day i've been googling for this? Aye. Welcome
Owen Stewart
Heh. Anyways, have fun OP. And if you want to be able to at least copy and paste things in the command prompt, try this.
And use the simple questions thread next time or you'll have people shitting on you.
Python is, but Windows and NTFS isn't. I'm pretty sure you can type pYThOn sCrIptNaMe.pY and have it still work, but not do the same from within Python itself.
Camden Nguyen
Will do, thanks again
Cooper Adams
escape `\` like so "C:\\User\\Aleksandr\\...
Charles Cox
Or use r prefixed raw strings. Or just use forward slashes, they should work everywhere except in cmd exe
Andrew Phillips
>Command line programming in Windows Found the problem. If you're gonna program in Windows, do it in a language where you can use all GUIshit. Or, install cygwin to build an environment similar to Linux and go from there. I'd say install Linux, but you don't seem like you'd really do too well with that.
William Harris
There is a russian version of Sup Forums that you might be interested in -> 2ch.hk
Alexander Roberts
download atom with the script plugin
Dominic Cruz
Since i live in the US i can't post on 2ch. I wish i could since they have a whole board dedicated to programming.
Kevin Robinson
yeah, they have a board even for radio tech! also, you can use vpn, can't you? i mean just for browsing imageboards speed restrictions shouldn't be a problem. try protonvpn. it's free as in free beer.
Brayden Green
>protonvpn Good advice, thank you! I'll look into it.